springMVC annotation enabled

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 entry 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 means the container is started When initializing this servlet, the smaller the positive value, the higher the priority -->
    <load-on-startup>1< /load-on-startup>
  </servlet>
  <!-- 拦截 -->
  <servlet-mapping>
   <servlet-name>springMVC</servlet-name>
   <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

 

<?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

 

 

 

 <!-- spring 启动时扫描包 -->
   <context:component-scan base-package="com.tgb.web.controller.annotation">
   </context:component-scan>
  
   <!-- 开启注解 -->
 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
 
 <!-- 路径对应调用的Controller -->
 <bean name="/test/helloworld" class="com.tgb.web.controller.HelloWorldController"/>
 <!-- 视图解析 -->
      <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 intercept access to things in this directory) -->
 <mvc:resources location=" /img/" mapping="/img/**" />

</beans>


Again, write the java code of the Controller

 

 

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;
@Controller
public 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);
 }
}

 

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

 

<%@ 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>
    <h1>SpringMVC注解</h1>
    <br>
    ${result }
  </body>
</html>


touser.jsp interface

 

 

 
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'index.jsp' starting page</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.     <!--  
  19.     <link rel="stylesheet" type="text/css" href="styles.css">  
  20.     -->  
  21.   </head>  
  22.     
  23.   <body>  
  24.     <form action="/springMVC4/user/addUser" method="post">  
  25.     <h1>SpringMVC annotations</h1>   
  26.     <br>  
  27.     ${result }  
  28.     <input type="submit"  value="post请求">  
  29.     </form>  
  30.   </body>  
  31. </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>

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326988798&siteId=291194637