Spring 之九 整合Spring MVC
2016-03-18 18:00
405 查看
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.jar
2.web.xml文件
3.Spring配置文件applicationContext.xml
4.Spring MVC配置文件dispatcher-servlet.xml
5.Controller文件
6.JSP文件
index.jsp
return.jsp
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.jar
2.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"> <!-- Spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--Spring MVC配置文件 使用Servlet监听Web请求--> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3.Spring配置文件applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!--controller文件不需扫描--> <context:component-scan base-package="com.ithings"> <context:exclude-filter type="regex" expression=".*Controller"/> </context:component-scan> <!-- 注册SpringContextUtil --> <bean class="com.ithings.utils.SpringContextUtil"/> </beans>
4.Spring MVC配置文件dispatcher-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.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!--只扫描controller文件--> <context:component-scan base-package="com.ithings"> <context:include-filter type="regex" expression=".*Controller"/> </context:component-scan> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
5.Controller文件
@Controller @RequestMapping(value="/login") public class LoginController { @RequestMapping(value="/return") public String goReturn(HttpServletRequest request, HttpServletResponse response){ return "return"; } }
6.JSP文件
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <a href="login/return">return</a> </body> </html>
return.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Return Success</h1> </body> </html>
相关文章推荐
- Java基础之equals详解
- Java 生成二维码
- java判断字符串是否乱码
- 斐波那契堆(Fibonacci heap)原理详解(附java代码实现)
- java 请求url并解析
- Spring mvc @ResponseBody返回JSON前台JSP乱码解决
- 2. Add Two Numbers | Java最短代码实现
- java mybatis 之 mysqlmap 动态 sql 语句
- 怎样增强MyEclipse的代码自动提示功能
- SpringMVC那点事
- Java并发 线程池
- Spring MVC里面的预防 SQL 注入
- java读写excel文件
- SpringMVC定义Interceptor
- json与javaBean的序列化与反序列化(待续...)
- Struts 2 和 JSF 的区别
- Eclipse关于设置code style的问题
- spring ioc(反转控制)
- jdk 编译器 对final字段的处理
- Java第二次实验