web.xml配置文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<!--
Servlet的加载时机, 如果是负数,在第一次使用的时候加载,如果是正数,
在服务器启动的时候,按照顺序从小到大,依次加载.
-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!--
这里注意,有两种写法,一种是 / ,当使用这种方法时注意springmvc.xml
另一种是*.do
-->
<url-pattern>*.do</url-pattern> <!-- / 支持 restful风格的url请求 -->
</servlet-mapping>
</web-app>
sprintmvc.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: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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--此配置文件为中央控制器配置文件-->
<!--组件扫描-->
<context:component-scan base-package="cn.csy.account.controller"></context:component-scan>
<!--导入配置文件-->
<import resource="classpath:spring/spring-*.xml"></import>
<!--添加注册驱动,解释方法中的注解(@RequestMapping("/hello"))-->
<!--通俗的说就是找到对应的方法-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--在tomcat中的web.xml文件中有两个servlet
一个是默认的访问静态资源
另一个是jsp的,访问jsp-->
<!--访问静态资源时重定向到tomcat中的web.xml,
通过Tomcat中的访问静态资源的servlet来访问项目中的静态资源
-->
<!--如果不写这个语句,则不会进行重定向,会将Tomcat中的
访问静态资源的servlet给覆盖掉,导致无法访问静态资源-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>
Controller层
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(){
System.out.println("hello mvc");
return "/WEB-INF/view/index.jsp";
}
}
注意:在Controller层中,类的上面一定哟啊添加@Controller注解,若是不加,则配置文件会找不到Controller层的类
并且在方法的上面要加上 @RequestMapping("/标识")方便路径的找到