`
xiaotao.2010
  • 浏览: 211857 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

SpringMVC初探_(1)

 
阅读更多

废话不说了,直接上代码

 

作为Spring MVC的初探篇章 ,首先把项目搭建起来,看看SpringMVC到底是什么东西

 

 

要想深入研究 Spring MVC ,推荐http://jinnianshilongnian.iteye.com/blog/1631021(开涛讲的SpringMVC很不错)

 

 依赖的jar包

----------------------------------------------------------------------------------------------------------

*1、  Spring框架jar包:

为了简单,将spring-framework-3.1.1.RELEASE-with-docs.zip/dist/下的所有jar包拷贝到项目的WEB-INF/lib目录下;

*2、  Spring框架依赖的jar包:

需要添加Apache commons logging日志,此处使用的是commons.logging-1.1.1.jar;

需要添加jstl标签库支持,此处使用的是jstl-1.1.2.jar和standard-1.1.2.jar;

 

 

----------------------------------------------------------------------------------------------------------

 

1、web.xml web项目的运行时的加载文件

 

 

 

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
	<display-name>test-webapp</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>
  	
	<filter>   
		<filter-name>CharacterEncodingFilter</filter-name>   
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>   
		<init-param>   
			<param-name>encoding</param-name>   
			<param-value>utf-8</param-value>   
		</init-param>   
	</filter>   
	<filter-mapping>   
		<filter-name>CharacterEncodingFilter</filter-name>   
		<url-pattern>/*</url-pattern>   
	</filter-mapping>  

	
	<servlet>   
		<!-- SpringMVC 的核心控制器 web运行环境中的名称 ,缺损加载路径时 下面{*}内容为该名称 -->
		<servlet-name>springServlet</servlet-name>  
		<!-- SpringMVC 的核心控制器,DispatcherServlet --> 
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>   
		<load-on-startup>1</load-on-startup> 
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- Spring MVC 加载时候 去寻找Spring MVC的配置文件 -->
			<!-- 以下是指定路径及文件名,缺损状态为{*}-servlet.xml文件  -->
			<param-value>/WEB-INF/spring-mvc-config.xml</param-value>
		</init-param>  
	</servlet>   
	<servlet-mapping>   
		<servlet-name>springServlet</servlet-name>   
		<url-pattern>/</url-pattern>   
	</servlet-mapping>  
	
</web-app>
 

 

 

2、spring-mvc-config.xml / {*}-servlet.xml SpringMVC运行时加载的配置文件

 

以下配置文件是以注解方式,注释内容中为非注解方式

 

 

<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!--
			the application context definition for the springappDispatcherServlet
		-->
	<!-- 默认的注解映射的支持 -->  
	<context:annotation-config />
	
	<!-- 启动时注解扫描器扫描包中的注解,目标包  base-package="*,*" 多个包以逗号分割-->  
	<context:component-scan base-package="com.test.controller" />

	<!--启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

	
	<!-- 在使用Spring MVC 映射配置文件时候使用 -->
	<!-- HandlerMapping  
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
 	-->
	<!-- HandlerAdapter
	<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
	 -->
	 
	 
	<!-- 视图解释类 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
		<property name="prefix" value="/jsp/"/><!-- 视图资源存放在 /jsp/的路径下 -->
		<property name="suffix" value=".jsp"/>
	</bean>
	
	<!-- 处理器  (非注解时使用)
	<bean name="/hello" class="com.test.controller.HelloWorldController"/>
	-->
</beans>
 

 

3、此时将该web工程部署在tomcat下,运行该项目,若木有出现问题 继续以下内容。

 

4、写第一个helloWorld(XML配置方式)

 

(1)先实现一个SpringMVC的控制器

HelloWorldController.java

 

package com.test.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

//需要实现 Controller接口(Spring提供了很多Controller接口)
public class HelloWorldController implements Controller {

	@Override
    public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
       //1、收集参数、验证参数
       //2、绑定参数到命令对象
       //3、将命令对象传入业务对象进行业务处理
       //4、选择下一个页面
       ModelAndView mv = new ModelAndView();
       //添加模型数据 可以是任意的POJO对象
       
       HashMap<String, String> hm = new HashMap<String, String>();
       hm.put("k1", "v1");
       hm.put("k2", "v2");
       mv.addObject("message", "Hello World!");
       mv.addObject("message2", "Hello World2!");
       mv.addObject("map",hm);
       //设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面
       mv.setViewName("hello");
       return mv;
    }


}
 

 

(2)展示页面

hello.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
<html>   
<head>   
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
<title>Hello World</title>   
</head>   
<body>   
${message}   
<br/>
${message2}  
<br/>
${map} 
<br/>
${rants}
</body>   
</html>  
 

 

(3) 填写配置文件 spring-mvc-config.xml

 

将spring-mvc-config.xml 文件中的注释部分释放,为了清楚了解 helloWorld ,下面将spring-mvc-config.xml重新写出,注意该spring-mvc-config.xml仅针对于Spring MVC 配置部分的实现,目前Spring MVC以注解为主,注解能大量减少配置文件的书写从而能够快速进行项目开发,注解缺点 对目标代码的嵌入型太强,不利于二次开发和修改。

 

 

<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


	<!-- 在使用Spring MVC 映射配置文件时候使用 -->
	<!-- HandlerMapping --> 
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
 	
	<!-- HandlerAdapter -->
	<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
	
	 
	 
	<!-- 视图解释类 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
		<property name="prefix" value="/jsp/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
	
	<!-- 处理器 ,配置helloWorldController控制器,响应映射为/hello (非注解时使用)-->
	<bean name="/hello" class="com.test.controller.HelloWorldController"/>
	
</beans>
 

(4)、访问运行

此时重启Tomcat ,打开浏览器 URL 输入http://localhost:8080/test-webapp/hello

正确显示内容为

写道

Hello World!
Hello World2!
{k1=v1, k2=v2}

 

 

5、写一个注解方式的demo(注解方式)

(1)控制器

HomePageController.java

 

 

package com.test.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller 
@RequestMapping("/hello")
public class HomePageController {
	
	@RequestMapping(params = "method=sayHello")
    public String sayHello(Model model) { 
        model.addAttribute("rants","hello,world");
        return "hello"; 
    } 

	@RequestMapping(params = "method=sayHelloHomePage")
    public String sayHelloHomePage(Model model) { 
        model.addAttribute("rants","spring MVC");
        return "hello"; 
    }     

}

 

 

(2)展示页面

为上 4—(2)中的hello.jsp

 

(3)填写配置文件 spring-mvc-config.xml

 

为上 2 中的 spring-mvc-config.xml

 

(4)访问运行

重启Tomcat ,打开浏览器 URL 输入http://localhost:8080/test-webapp/hello?method=sayHello

正确显示内容为

 

 写道



hello,world
 

重启Tomcat ,打开浏览器 URL 输入http://localhost:8080/test-webapp/hello?method=sayHelloHomePage

 

正确显示内容为

 

 写道



spring MVC
 

--------------------------------------------------------------------------------------------------

 

转发与重定向

可以通过redirect/forward:url方式转到另一个Action进行连续的处理。

可以通过redirect:url 防止表单重复提交 

写法如下:

return "forward:/order/add";

return "redirect:/index.jsp";

分享到:

相关推荐

Global site tag (gtag.js) - Google Analytics