SPRINGMVC Annotation and optimization

Source: Internet
Author: User

1. Create a new Web project

2. Add jar

3. Rewrite Web. XML, note the name of Spring-servlet.xml

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "3.0" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "> <display-name></display-name> < welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet&  Gt <servlet-name>springMVC</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name> Contextconfiglocation</param-name> <param-value>classpath*:config/springannotation-servlet.xml</ param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <  Servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </wEb-app> 

4. Write Springannotation-servlet.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "H Ttp://www.springframework.org/schema/context "xmlns:mvc=" Http://www.springframework.org/schema/mvc "Xsi:sche malocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/sche Ma/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.sprin                Gframework.org/schema/context/spring-context-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC  Http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <!--Note Scan Package--<context:component-scan Base-package= "Com.tgb.web.controller.annotation"/> <!--opening annotations-<bean class= "Org.springframew Ork.web.servlet.mvc.annotation. Annotationmethodhandleradapter "></bean> <bean class=" Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping "></bean> <!--static resource access- -<mvc:resources location= "/img/" mapping= "/img/**"/> <mvc:resources location= "/js/" mapping= "/js/**"/&    Gt <mvc:resources location= "/css/" mapping= "/css/**"/> <!--viewresolver View Resolver--<bean id= "vie Wresolver "class=" Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property name=" Prefix "value="/web-inf/jsp/"/> <property name=" suffix "value=". jsp "/> </bean> &LT;/BEANS&G T

5. New package com.tgb.web.controller.annotation, new class Usercontroller.java

Note how annotations are annotated, class annotations, and method annotations

Package Com.tgb.web.controller.annotation;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import org.springframework.web.servlet.modelandview;@ Controllerpublic class Usercontroller {@RequestMapping (value= "/user/adduser", method=requestmethod.get) public Modelandview AddUser () {String result = "This is AddUser---------"; return new Modelandview ("/annotation", "result", result); } @RequestMapping (value= "/user/deluser", method=requestmethod.get) public Modelandview Deluser () {String result = ' This is Deluser---------"; return new Modelandview ("/annotation "," result ", result); }}

6. Under Spring-servlet.xml settings, create a new annotation.jsp under/web-inf/jsp/

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><% @taglib prefix= "C" uri= "/HTTP/ Java.sun.com/jsp/jstl/core "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

 

7. IE Side access:

Http://localhost:8080/springMVC3/user/addUser

Show Results:

Annotation Notes 1  

  

  

SPRINGMVC Annotation and optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.