This article is my study of the network video SPRINGMVC writing process.
Thank you for announcing the video of your predecessors.
The following comments Springmvc several key steps, notes enable:
First you need to load the configuration file (assuming you use a custom path)
<?XML version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= " Http://java.sun.com/xml/ns/javaee "xmlns:web=" http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "Webapp_ ID "version=" 2.5 "> <display-name>springMVC1</display-name> <welcome-file-list> < Welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> < Welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> < Welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </ welcome-file-list> <!--SPRINGMVC Entrance Dispatcher--<servlet> <servlet-name>springmvc</ Servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--loadInto the configuration file path-<init-param> <param-name>contextConfigLocation</param-name> <param-value> Classpath*:config/springannotation-servlet.xml</param-value> </init-param> <!--when to start A value greater than 0 indicates that the servlet is initialized when the container starts, the smaller the positive value the higher the priority-<load-on-startup>1</load-on-startup> </servlet> <!-- Intercept-<servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</ Url-pattern> </servlet-mapping></web-app>
Second, the configuration file. The most basic is to open annotations and load the scan package when spring starts
<?xml version= "1.0" encoding= "UTF-8"?><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:context= "http://www.springframework.org/ Schema/context "xmlns:p=" http://www.springframework.org/schema/p "xmlns:mvc=" http://www.springframework.org/ Schema/mvc "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation="/HTTP/ Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http:/ /www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd htt P://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "><!-- Spring Boot Scan Package--<context:component-scan base-package= "Com.tgb.web.controller.annotation" > </context: component-scan> <!--opening annotations--><bean class= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "/><bean class=" Org.springframework.web.servlet.mvc.aNnotation. Defaultannotationhandlermapping "></bean><!--path controller--><bean name="/test/helloworld " class= "Com.tgb.web.controller.HelloWorldController"/><!--View Resolution--<bean id= "Viewresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property name=" prefix "value="/web-inf /jsp "></property> <property name=" suffix "value=". jsp "></property> </bean> <!--static resource access (do not block access to things under this folder)--><mvc:resources location= "/img/" mapping= "/img/**"/> </beans>
Again, write the controller's Java code
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.post) public Modelandview AddUser () {String result = "AddUser"; return new Modelandview ("/annotation", "result", result);} @RequestMapping (value= "/user/deluser", method=requestmethod.get) public Modelandview Deluser () {String result = " Deluser "; return new Modelandview ("/annotation "," result ", result);} @RequestMapping (value= "/user/touser", method=requestmethod.get) public Modelandview Touser () {String result = "Touser" ; return new Modelandview ("/touser", "result", result);}}
Please note here that the use of @Controller.
The value in the @RequestMapping (value= "/user/adduser", Method=requestmethod.post) represents a jump path, which means that the method is called in which way
Finally, the front-end JSP interface
annotation.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Touser.jsp interface
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><%string path = Request.getcontextpath () ; String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
SPRINGMVC annotation Enabled