Creation of spring MVC entry program (annotation mode)

Posted by phpScott on Wed, 13 Nov 2019 16:26:54 +0100

The annotation method is the same as the configuration file method, so there are different points written here and the configuration file method.

Profile to create a connection for springMVC: Portal

The web.xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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_2_5.xsd">
  
  <!-- Configure front end controller -->
  <servlet>
  	<servlet-name>springAnno</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:/springAnno-servlet.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springAnno</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

When writing the processor, you can directly use the annotation @ RequestMapping() on the placement without implementing the Controller interface

Processor code:

package cn.test.temp;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Second {	
	@RequestMapping("/second.action")
	public String test(Model model){
		model.addAttribute("msg1","hello springMVC");
		model.addAttribute("msg2","hello boys....");
		return "second";
	}
}

Note: to use this annotation, you need to enable the annotation function of mvc. To enable the annotation function of mvc, you need to add: xmlns:mvc in the core configuration file=“ http://www.springframework.org/schema/mvc "Rules"

Then the configuration of the core configuration file is as follows

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		xmlns:context="http://www.springframework.org/schema/context" 
		xmlns:aop="http://www.springframework.org/schema/aop" 
		xmlns:mvc="http://www.springframework.org/schema/mvc"
		xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	
	<!-- Turn on package scanning -->
	<context:component-scan base-package="cn.test.temp"></context:component-scan>
	
	<!-- open springMVC Annotation mode -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- Configure view resolver -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
		
</beans>

Code of view parser second.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>    
    <title>second</title>
  </head>
  <body>
    ${msg1} <br>
    ${msg2} <br>
    <%=new Date().toLocaleString()%>
  </body>
</html>

Test results:

Topics: xml Java Spring JavaEE