Spring MVC 的初步应用
2016-07-23 15:23
393 查看
1.首先导入所需的 jar包
2.在web.xml里配置前端控制器和拦截器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="
3.配置SpringMVC
3.1 方法一 手动配置
springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd">
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
</bean>
<bean id="/hello.action" class="com.smvc.action.HController"> </bean>
<pre name="code" class="html"> <!-- <span style="font-family: Arial, Helvetica, sans-serif;"> 配置处理映射器 </span><span style="font-family: Arial, Helvetica, sans-serif;"> --></span> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 手动添加action的bean
-->
<bean class="com.smvc.action.HController"></bean> <!-- 引入解析jstl的类 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean></beans>
HController.java
package com.smvc.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest requset,
HttpServletResponse response) throws Exception {
ModelAndView m=new ModelAndView(); //向页面传数据..
m.addObject("hello","haha");
m.setViewName("/index.jsp");
return m;
}
}
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>springMVC</title>
</head>
<body>
${hello}<br>
</body>
</html>
3.2 方法二 用扫描和注释
web.xml 不变
springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd">
<context:annotation-config>
<mvc:annotation-driven>
</mvc:annotation-driven>
</context:annotation-config>
<!-- 用扫描的方式把包加入到bean 中 -->
<context:component-scan base-package="com.smvc.action"></context:component-scan>
<!-- 引入解析jstl的类 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>
testAction.java
package com.smvc.action;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class testAction {
/*
* 注释方法
*/
@RequestMapping(value="/test.action") //地址映射的注释
public ModelAndView gettest(){
ModelAndView m=new ModelAndView(); //向页面传值用。。。
m.addObject("hello", "leaf");
m.setViewName("/index.jsp");
return m;
}
}
jsp页面同上
2.在web.xml里配置前端控制器和拦截器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="
http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>springMVC</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 配置前端控制器 --> <servlet> <servlet-name>springmvc2</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc2</servlet-name> <url-pattern>*.action</url-pattern> <!--配置的访问路径,一定是按照这种格式写 --> </servlet-mapping> </web-app>
3.配置SpringMVC
3.1 方法一 手动配置
springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd">
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
</bean>
<bean id="/hello.action" class="com.smvc.action.HController"> </bean>
<pre name="code" class="html"> <!-- <span style="font-family: Arial, Helvetica, sans-serif;"> 配置处理映射器 </span><span style="font-family: Arial, Helvetica, sans-serif;"> --></span> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- 手动添加action的bean
-->
<bean class="com.smvc.action.HController"></bean> <!-- 引入解析jstl的类 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean></beans>
HController.java
package com.smvc.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest requset,
HttpServletResponse response) throws Exception {
ModelAndView m=new ModelAndView(); //向页面传数据..
m.addObject("hello","haha");
m.setViewName("/index.jsp");
return m;
}
}
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>springMVC</title>
</head>
<body>
${hello}<br>
</body>
</html>
3.2 方法二 用扫描和注释
web.xml 不变
springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd">
<context:annotation-config>
<mvc:annotation-driven>
</mvc:annotation-driven>
</context:annotation-config>
<!-- 用扫描的方式把包加入到bean 中 -->
<context:component-scan base-package="com.smvc.action"></context:component-scan>
<!-- 引入解析jstl的类 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>
testAction.java
package com.smvc.action;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class testAction {
/*
* 注释方法
*/
@RequestMapping(value="/test.action") //地址映射的注释
public ModelAndView gettest(){
ModelAndView m=new ModelAndView(); //向页面传值用。。。
m.addObject("hello", "leaf");
m.setViewName("/index.jsp");
return m;
}
}
jsp页面同上
相关文章推荐
- Java编程思想第4版-第一章
- 最大公约数问题的优化解法
- eclipse debug JDK中源代码时出现 missing line number
- 什么是 Java 中的内部类
- New package not yet registered with the system
- JDK里面常用的包
- linphone-LpConfig.java文件分析
- Java进阶(三十六)深入理解Java的接口和抽象类
- Java进阶(三十六)深入理解Java的接口和抽象类
- java方法(基础知识)
- java设计模式之简单工厂模式
- 【原创】遨游springmvc之DispatcherServlet
- 坦克大战_坦克绘制
- Eclipse修改包的显示样式
- 【原创】遨游springmvc之原理篇
- java常用类(基础)
- IT--java--jdk参数设置
- Java基础之进制
- MyEclipse——快捷键的使用
- java的内部类