spring mvc: internal resource view resolver (annotation implementation) @Controller/@RequestMapping
Project access address:
http://localhost:8080/guga2/hello/print
or
http://localhost:8080/guga2/hello
Annotation classes used:
org.springframework.stereotype.Controller; org.springframework.web.bind.annotation.RequestMapping; org.springframework.web.bind.annotation.RequestMethod;
view model class
org.springframework.ui.ModelMap;
In this example, the package name: springweb
There are three xml configuration files in this example: web.xml, applicationContext.xml, xxxx-servlet.xml
web.xml
<!--Configuration file path--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- character filter --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Monitor forwarding--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springweb</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springweb</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- default: annotation mapping support --> <mvc:annotation-driven/> <!-- Static resource configuration--> <mvc:resources location="/pages/**" mapping="/pages/"/> <!-- Automatically scan package name, controller --> <context:component-scan base-package="springweb"/> </beans>
springweb-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- view --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
jsp code
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page isELIgnored="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>hello world</title> </head> <body> ${message} </body> </html>
HelloController.java code
1. Project/hello access code
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.ui.ModelMap; @Controller @RequestMapping(value="/hello") public class HelloController { @RequestMapping( method=RequestMethod.GET) public String printHello(ModelMap model) { model.addAttribute("message", "spring mvc framework maps web pages and request instances"); return "hello"; } }
2. Project/hello/print access code
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.ui.ModelMap; @Controller public class HelloController { @RequestMapping(value="/hello/print", method=RequestMethod.GET) public String printHello(ModelMap model) { model.addAttribute("message", "spring mvc framework maps web pages and request instances"); return "hello"; } }