- 浏览: 86798 次
- 性别:
- 来自: 杭州
最新评论
-
lecheng1986:
代码求分享下
java 应用的并发与流量控制 -
Sean_feng.wei:
accplxmaccplxm 写道源码从哪里下载呀?
新书-大型分布式网站架构设计与实践 -
quainter:
西欧字符改为中欧字符就好了
eclipse中文注释文字较小解决办法 -
wangguanghua:
不错。。。
浅析http平台的安全稳定性架构 -
java_workblog:
期待,又可以学好东西了.
复活贴
项目地址:http://www.springsource.org/
文档地址: http://www.springsource.org/documentation
下载: http://www.springsource.org/download
spring mvc 简介:
Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。spring mvc 实现的功能类似struts 提供的mvc 解决方案,但是比spring 更加轻量级且更加灵活。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
Spring
的 Web
MVC
框架是围绕 DispatcherServlet
设计的,它把请求分派给处理程序,同时带有可配置的处理程序映射、视图解析、本地语言、主题解析以及上载文件支持。默认的处理程序是非常简单的
Controller
接口,只有一个方法 ModelAndView handleRequest(request, response)
。
由于不在本文讨论范畴,为了方便起见,笔者仅对dao层和service层做了简单的实现,如需连接数据源,请读者自行修改。
所需jar包:
web.xml 配置:
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>Spring character encoding filter</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>Spring character encoding filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping>
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:p="http://www.springframework.org/schema/p" 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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 自动搜索 所有配置的autowired或者resource都是在该目录下面--> <context:component-scan base-package="com"></context:component-scan> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- @Autowired --> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/page/" /> <property name="suffix" value=".jsp" /> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="i18n/messages" /> <!-- 配置rest,根据不同请求,做不同的响应 如 /blog/1.json /blog/1.xml--> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <!-- 在没有扩展名时即: "/user/1" 时的默认展现形式 --> <property name="defaultContentType" value="application/xml" /> <!-- 扩展名至mimeType的映射,即 /user.json => application/json --> <property name="mediaTypes"> <map> <entry key="html" value="text/html" /> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> </map> </property> <property name="defaultViews"> <list> <!-- 输出为JSON数据--> <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"> <property name="prefixJson" value="false" /> </bean> <!-- 输出为xml数据--> <bean id="marshallingView" class="org.springframework.web.servlet.view.xml.MarshallingView"> <constructor-arg> <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <property name="classesToBeBound"> <array> <value>com.mvc.domain.User</value> <value>com.mvc.domain.UserList</value> </array> </property> </bean> </constructor-arg> </bean> </list> </property> </bean> <!-- 使用resource标签 --> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> </beans>
contoller 代码:
package com.mvc.controller; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.mvc.domain.User; import com.mvc.domain.UserList; import com.mvc.service.UserService; /** * @Project: spring_mvc_test * * * @Annotation: 用户controller * * @Date:2011-9-5 * */ @Controller @RequestMapping("/user") public class UserController{ //@Resource(type=com.mvc.service.UserServiceImpl.class) @Autowired private UserService userServiceImpl; @InitBinder public void initBinder(WebDataBinder binder) { //设置日期转换 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); dateFormat.setLenient(false); binder.registerCustomEditor(Date.class, new CustomDateEditor( dateFormat, false)); } @RequestMapping("hello") public ModelAndView hello(){ ModelAndView mav = new ModelAndView(); mav.setViewName("hello"); mav.addObject("message", "hello every body !"); return mav; } @RequestMapping("register") public ModelAndView register(){ ModelAndView mav = new ModelAndView(); mav.setViewName("register");//后台的jsp页面名称 return mav; } /** * * 传递表单内容 * * Last Modification Time: 2011-9-5 * * @param request * @param response * @param user 传递过来的用户 * @return */ @RequestMapping(value="/show", method = RequestMethod.POST) public ModelAndView showUser(HttpServletRequest request,HttpServletResponse response,User user){ System.out.println("host: " + request.getRemoteHost()); System.out.println("userinfo: " + user); ModelAndView mav = new ModelAndView(); mav.setViewName("display");//后台的jsp页面名称 mav.addObject("birthday",user.getBirthday()); mav.addObject("id",user.getId()); mav.addObject("money",user.getMoney()); mav.addObject("name",user.getName()); mav.addObject("user",user); userServiceImpl.addUser(user); return mav; } @RequestMapping("/find/{id}") public ModelAndView getUserById(@PathVariable int id,HttpServletRequest request,HttpServletResponse response){ ModelAndView mav = new ModelAndView(); mav.setViewName("display");//后台的jsp页面名称 System.out.println("id: " + id); User user = new User(); user.setName("aaaaa"); mav.addObject("user",user); return mav; } /** * 跳转到doadd的jsp页面 * * * Last Modification Time: 2011-9-5 * * @return */ @RequestMapping("/doaddview") public ModelAndView doaddview(){ ModelAndView mav = new ModelAndView(); mav.setViewName("add");//后台的jsp页面名称 return mav; } @RequestMapping(value="add") public String doadd(HttpServletRequest request,HttpServletResponse response){ System.out.println("Hello aaaaaaa "); request.setAttribute("message", "Hello aaaaa ! ,@RequestMapping(value='/add')"); return "hello"; } /** * 通过这种方法也可以获得user * * Last Modification Time: 2011-9-5 * * @param user */ @RequestMapping(value="add") public void addUser(@ModelAttribute("user") User user){ System.out.println("controller 级联"); System.out.println(user.getName()); } @RequestMapping("/showuser") public ModelAndView getById(){ User user = new User(); user.setBirthday(new Date()); user.setId(111); user.setMoney(112.0f); user.setName("aaaaaaaaa"); return new ModelAndView("display", "user", user); } @RequestMapping("/showsth") public ModelAndView getByIdAndName(){ List list = new ArrayList(); User user = new User(); user.setBirthday(new Date()); user.setId(111); user.setMoney(112.0f); user.setName("aaaaaaa"); list.add(user); list.add(user); list.add(user); list.add(user); list.add(user); list.add(user); UserList userList = new UserList(); userList.setList(list); userList.setName("UserList"); return new ModelAndView("display", "userlist", userList); } }
user dao 代码:
package com.mvc.dao; import com.mvc.domain.User; /** * @Project: spring_mvc_test * * @Annotation: 用户dao * * @Date:2011-9-5 * */ public interface UserDAO { public void addUser(User user); public User findUser(int id); }
user dao impl 代码:
package com.mvc.dao; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Repository; import com.mvc.domain.User; /** * @Project: spring_mvc_test * * @Annotation: user dao 的实现类, * Data Access Object or DAO * 使用@Repository来标识 * * @Date:2011-9-5 * */ @Repository @Scope("prototype") public class UserDAOImpl implements UserDAO{ @Override public void addUser(User user) { } @Override public User findUser(int id) { return null; } }
user 类代码:
package com.mvc.domain; import java.util.Date; import javax.xml.bind.annotation.XmlRootElement; import com.thoughtworks.xstream.annotations.XStreamAlias; /** * @Project: spring_mvc_test * * @Annotation: 用户 * * @Date:2011-9-5 * */ @XmlRootElement(name="user") public class User { private int id; private String name; private Date birthday; private float money; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public float getMoney() { return money; } public void setMoney(float money) { this.money = money; } public User() {} public User(String name) { this.name = name; } public User(float money) { this.money = money; } public void showName() { System.out.println(this.name); } @Override public String toString() { return "id=" + this.id + " name=" + this.name + " birthday=" + this.birthday + " money=" + this.money; } }
userlist 代码:
package com.mvc.domain; import java.util.List; import javax.xml.bind.annotation.XmlElements; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** * @Project: spring_mvc_test * * @Annotation: * * @Date:2011-9-7 * */ @XmlRootElement public class UserList { private String name; private List list; @XmlElements({ @XmlElement(name = "user", type = User.class) }) public List getList() { return list; } public void setList(List list) { this.list = list; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
user service 代码:
package com.mvc.service; import com.mvc.domain.User; /** * @Project: spring_mvc_test * * * @Date:2011-9-5 * */ public interface UserService { public void addUser(User user); public User findUser(int id); }
user service impl 代码:
package com.mvc.service; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.mvc.dao.UserDAO; import com.mvc.domain.User; /** * @Project: spring_mvc_test * * @Annotation: userservice 实现类 * 使用@Service标注service * * @Date:2011-9-5 * */ @Service public class UserServiceImpl implements UserService { //@Resource(type=com.mvc.dao.UserDAOImpl.class) @Autowired private UserDAO userDAOImpl; @Override public void addUser(User user) { System.out.println("调用userservice的addUser方法"); userDAOImpl.addUser(new User()); } @Override public User findUser(int id) { System.out.println("调用userservice的findUser方法"); userDAOImpl.findUser(111); return null; } }
发表评论
-
websocket与comet的性能对比
2016-01-02 14:34 2553对于comet长连接来说,服务端如有内容需要发送给客户端时 ... -
新书-大型分布式网站架构设计与实践
2014-08-28 19:51 3463第1章 面向服务的体系架构(soa) 1 本章主要 ... -
浅析http平台的安全稳定性架构
2013-03-01 14:47 3677--转载自《程序员》2013年第2期 前言:随着移动互联 ... -
转: Base64编码
2012-09-21 16:39 1348什么是Base64? 按照RFC2045的定义,Base64被 ... -
spring整合ibatis事务管理
2011-11-17 14:52 3422Spring通过DAO模式,提供了对iBATIS的良好支持。S ... -
mustache模板技术
2011-06-28 11:37 4162项目首页:http://mustache.github.com ...
相关推荐
第3篇是Spring MVC技术入门,包括Spring MVC的背景介绍、架构整体剖析、环境搭建、处理器与映射器的讲解、前端控制器的源码分析、多种视图解析器的介绍、请求映射与参数绑定的介绍、Validation校验与异常处理和拦截...
本书由浅入深地介绍了当今流行的Java Web框架Spring MVC的方方面面,从基础的环境搭建到微服务设计与架构,再到持久化、REST API构建、认证与测试……涵盖了Spring MVC诸多重要且常用的特性。值得一提的是,本书针对...
《深入解析Spring MVCgn Web Flow》是Spring MVC 和Web Flow 两个框架的权威指南,书中包括的技巧和提示可以让你从这个灵活的框架中汲取尽可能多的信息。书中包含了一些开发良好设计和解耦的Web 应用程序的最佳实践...
深入解析Spring MVC与Web Flow.7z.002
详细的Spring MVC教程,内容包括控制器 重定向 保存并获取属性参数 拦截器 表单标签和验证表单 视图和视图解析器 上传文件 数据转换和格式化
Spring MVC--5.视图和视图解析器
thymeleaf,我个人认为是个比较好的模板,性能也比一般的,比如freemaker的要...由于thymeleaf在内存缓存解析后的模板,解析后的模板是基于tree的dom节点树,因此thymeleaf适用于一般的web页面,不适合基于数据的xml。
本地化:Spring MVC的本地化解析器 本地化:Spring MVC的本地化解析器 LocaleChangeInterceptor:通过URL参数指定 静态资源处理 静态资源处理:使REST风格的URL成为实现 静态资源处理:原理 静态资源处理:如何配置? ...
深入解析Spring MVC与Web Flow
奉上学习Spring MVC 3.0的技术资料PPT资料,我花了两天写成,该资料根据最新拙作的《Spring 3.x 企业应用开发实战 》写成,仅需要1个小时左右的时间,就可以让你学习到Spring MVC 3.0的所有知识点和新功能,强烈推荐...
Spring MVC源码深度剖析开源架构源码2021.pdf
此中包含了大量的SpringMVC demo, 以及spring mvc 部分源码解析。
Spring MVC是Spring框架中的一部分,全称是Spring Web MVC,主要用于实现MVC设计模式的Web框架。它分离了控制器、模型对象、过滤器以及处理程序...此外,Spring MVC还支持本地化/国际化(Locale)解析及文件上传等。
spring mvc web_flow xml版
Spring源代码解析(四):Spring MVC Spring源代码解析(五):Spring AOP获取Proxy Spring源代码解析(六):Spring声明式事务处理 Spring源代码解析(七):Spring AOP中对拦截器调用的实现 Spring源代码解析(八):...
_第一课:spring mvc 原理深度解析1
本代码使用了Spring MVC框架(spring2.5架包) 演示了(Controller接口的试用方法)和 MultiActionController多动作控制器 数据库连接试用Spring JDBC 并且着重介绍了MultiActionController多动作控制器的两种方法名...
整合Spring +Spring MVC +Mybatis ,部署eclispe(maven) 可以直接运行
Spring Web model-view-controller (MVC)框架是围绕 DispatcherServlet 设计的,并分发请求到处理程序(handler),Spring MVC支持可配置的处理程序映射(handler mapping),视图解析(view resolution)、 区域设置...
Spring源代码解析4:Spring MVC ;Spring源代码解析5:Spring AOP获取Proxy;Spring源代码解析6:Spring声明式事务处理 ; Spring源代码解析7:Spring AOP中对拦截器调用的实现 Spring源代码解析8:Spring驱动...