这些天一直在学习RequestMapping,学了好几天终于基本搞明白了,Talk is cheap ,let’s put the code:
这是一个RequestMapping的示例:
里面有三个配置文件:applicationContext.xml(还没搞清楚具体用途)、web.xml、mvc-servlet.xml
web配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>RequestMappingDemo2</display-name>
<!-- 定义spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 定义servlet -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<!--匹配全路径下的请求-->
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
mvc-servlet配置文件:
<?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:p="http://www.springframework.org/schema/p"
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">
<!-- 扫描controller包,应用特定注解 -->
<context:component-scan base-package="com.demo.controller"></context:component-scan>
<!-- 注解驱动 --><!-- 匹配静态资源 --><!--请一定注意,像html、css、jpg等格式的请求都是静态的资源请求,需要指定配置才能够由spring容器处理,当然,也可以不由其处理-->
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:resources mapping="/WEB-INF/html/**" location="/WEB-INF/html/"/>
<!--解析返回字符串 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/html/"/>
<property name="suffix" value=".html"/>
</bean>
</beans>
applicationContext配置文件:
<?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:p="http://www.springframework.org/schema/p"
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">
</beans>
控制器只有一个:UserControl,里面有两个方法,index()是用于处理默认页面请求,而second()是用于处理跳转页面请求:
package com.demo.controller;
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 UserControl {
@RequestMapping(value="/second")
public String second(){
System.out.println("second() is invoked !");
return "second";
}
//处理首页请求
@RequestMapping(value="/")
public String index(){
System.out.println("index() is invoked !");
return "first";
}
}
首页first:
<html>
<body>
This is the first page ! To go to the second page,please
<!-- 表单没有提交任何数据的时候不要指定提交方式,否则会报错 -->
<form action="second">
<input type="submit" value="click"/>
</form>
</body>
</html>
第二页second:
<html>
<body>This is the second page !</body>
</html>