This article is written by me in the process of learning the web video SpringMVC. Thanks to everyone who posted the video
The following explains several key steps for enabling SpringMVC annotations:
First, you need to load the configuration file (if you use your own code, please define the path yourself)
web.xm;
<?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 入口 dispatcher --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Load 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 this servlet is initialized when the container starts, the smaller the positive value, the higher the priority--> <load-on-startup>1</load-on-startup> </servlet> <!-- interception--> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
Secondly, the configuration file, the most important thing is to open the annotation and load the scanning package when Spring starts
Again, write the java code of the Controller
Note the use of @Controller here. The value in @RequestMapping(value="/user/addUser",method=RequestMethod.POST) indicates the jump path, and the method indicates which way to call this method
Finally, the jsp interface in the foreground
annotation.jsp
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">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <form action="/springMVC4/user/addUser" method="post">
- <h1>SpringMVC annotations</h1>
- <br>
- ${result }
- <input type="submit" value="post请求">
- </form>
- </body>
- </html>
<%@ 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"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="/springMVC4/user/addUser" method="post"> <h1>SpringMVC annotations</h1> <br> ${result } <input type="submit" value="post请求"> </form> </body> </html>