一 @RequestMapping
在类上定义:提供初步的请求映射信息。相对于 WEB 应用的根目录
在方法上定义:提供进一步的细分映射信息。相对于类定义处的 URL
下面是一个例子
首先 1.导入相关的jar包
commons-logging-1.1.3.jar
– spring-aop-4.0.0.RELEASE.jar
– spring-beans-4.0.0.RELEASE.jar
– spring-context-4.0.0.RELEASE.jar
– spring-core-4.0.0.RELEASE.jar
– spring-expression-4.0.0.RELEASE.jar
– spring-web-4.0.0.RELEASE.jar
– spring-webmvc-4.0.0.RELEASE.ja
2.修改web.xml(在web.xml文件中添加 dispatcherServlet
<servlet>
<servlet-name>dispatchServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatchServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.配置springmvc.xml文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置Controller扫描 -->
<context:component-scan base-package="org.springmvc" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
4. 写一个类来测试
package org.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld {
@RequestMapping(value="/helloA")
public String hello(){
System.out.println("helloword");
return "success";
}
}
测试的目录结构和hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<a href="helloA">helloworld</a><br>
<!-- <a href="helloAAA">test</a><br>
<form action="helloAAA" method="POST">
<input type="submit" value="POST"/>
</form>
<br>
<a href="testPH/?username=atguigu&age=40">testParams and headers</a><br> -->
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<h1>yes Success</h1>
</body>
</html>
启动tomcat 访问http://localhost:8080/springMVC-01/hello.jsp
点击helloworld 跳转到http://localhost:8080/springMVC-01/helloA
这里的helloA是RequestMapping里面的 .jsp作为后缀
若在测试类上添加@requestMapping
@RequestMapping("/cccc")
@Controller
public class HelloWorld {
@RequestMapping(value="/helloA")
public String hello(){
System.out.println("helloword");
return "success";
}
}
则必须要访问 http://localhost:8080/springMVC-01/cccc/helloA -------才可以访问到 success.jsp