1.导包
此过程可以导入相关SpringMVC的jar包,也可以直接搭建Spring环境进行这一步骤的操作
我用的myeclipes进行傻瓜式导包
2.配置web.xml
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.配置-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: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.1.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
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<!-- 扫描组件,将加上@Controller注解作为SpringMVC控制器 -->
<context:component-scan base-package="com.baidu.www" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
4.定义一个控制器类
package com.baidu.www;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping(value="hello")
public String hello() {
System.out.println("success");
return "success";//视图名称
}
}
5.创建一个success.jsp
在/WEB-INF/view/目录下创建一个success.jsp
6.测试
在浏览器输入localhost:8080/projectName/
会直接跳转到index.jsp
我在jsp中写了
<a href="hello">Test</a>
当点击超链接的时候,就会跳转到localhost:8080/projectName/hello,当controller类接收到请求时进行处理,根据return返回值,进行相应的页面跳转即:localhost:8080/projectName/前缀/返回值.后缀==localhost:8080/projectName/前view/success.jsp