1.web.xml中核心的servlet的配置
<!--配置核心的Servlet-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--告诉他 配置文件的路径在哪里?-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<!--下面表示只是拦截.action请求-->
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
2.编写配置文件
如果该配置文件在WEB-INF下, 则注意名字:核心Servlet在web.xml中名字加上 -servlet.xml 文件名 例如:DispatcherServlet -servlet.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd"
>
<!--告诉他扫描路劲---Contoller所在的路径-->
<context:component-scan base-package="com.qf.controller"></context:component-scan>
<!--使能这个注解---让这个注解生效-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
3.编写controller文件
@Controller //表示的是当前这个类是一个控制器
@RequestMapping("/users") //当前这个控制器 映射的大的路径
public class UserController {
@RequestMapping(value = "aa") //当前的方法映射的路径(小的路径)
public String aa(){
System.out.println("我是中国人...");
//表示返回的页面
return "/bobo.jsp";
}
}
4.web下创建一个bobo.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
获取到前端的数据是:${userName}
</body>
</html>