(1)先配置好web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--configure the setting of springmvcDispatcherServlet and configure the mapping-->
<servlet>
<servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- <load-on-startup>1</load-on-startup> -->
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
(2)我配置的是在src路径下去找的springmvc-servlet.xml文件。因此,创建在src下边创建springmvc-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: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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- 激活标签,生命周期的管理-->
<context:annotation-config/>
<!-- dispatcherServlet去在这里搜索com.hmy.ssh.Controller包下边的东西控制器和请求联系-->
<context:component-scan base-package="com.hmy.ssh.Controller"/>
<!-- don't handle the static resource -->
<mvc:default-servlet-handler />
<!-- 默认的注解映射的支持,if you use annotation you must configure following setting hadlermapping必须用这个-->
<mvc:annotation-driven />
<!-- 视图解释类configure the InternalResourceViewResolver,告诉用哪个viewresolver获取view -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 ,jsp文件在当前根目录下。可指定路径-->
<property name="prefix" value="/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
</bean>
<!-- 可能这个标签的真谛就是为了引用资源的访问不会类似CONTROLLER一样被拦截,区分出关注的资源的访问,
一般我们在springMVC里面的拦截都会配置为"/",拦截所有的。 -->
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/data/" mapping="/data/**"/>
</beans>
<!-- 拦截器 -->
<!--
<mvc:interceptors>
<bean class="com.hmy.ssh.MyInteceptor" />
</mvc:interceptors> -->
(3)创建controller类开始处理
package com.hmy.ssh.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
//告诉mvc是一个controller
@RequestMapping("/hello")
//拦截根目录下的hello
public class MyTestController {
@RequestMapping("/mvc")
//localhost:8888/hello/mvc
//即可,映射去到了home,代表了home.jsp
public String HelloMvc(){
return "jsp/home";//这里,如果是webroot下边的jsp文件夹下边的jsP文件,则要写明路径,否则404错误。
}
}
(4)打开网页,成功跳转
http://localhost:8888/CourseOnline/hello/mvc