临时应急做了两个月的ASP.NET,终于又回到Java上来了,还是Java感觉亲切啊。马上要开发一个新的项目,最近感觉spring mvc势头比较猛,就了解了一下,以前觉得spring mvc用起来较麻烦,所以一直用struts2,但了解了一下spring3 mvc,一下子就喜欢上了它,下个项目决定就用它了,RESTful URL、几乎0配置、不需要实现任何接口或继承任何类的Controller、方法级别的拦截,一个方法对应一个url、灵活的方法参数和返回值、多种view、处理ajax的请求更是方便...
下面的小例子用了spring mvc和hibernate,只是简单的用户增删改查,没有用ajax,ajax的版本在这里:Spring3 MVC + jQuery easyUI 做的ajax版本用户管理(http://www.iteye.com/topic/1081739),给和我一样准备用spring mvc的朋友参考一下吧。jar包如图:
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配置文件是在WEB-INF下的applicationContext.xml --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>Set Character Encoding</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value><!-- 强制进行转码 --> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 默认所对应的配置文件是WEB-INF下的{servlet-name}-servlet.xml,这里便是:spring3-servlet.xml --> <servlet> <servlet-name>spring3</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring3</servlet-name> <!-- 这里可以用 / 但不能用 /* ,拦截了所有请求会导致静态资源无法访问,所以要在spring3-servlet.xml中配置mvc:resources --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
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: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/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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName"> <!-- 注意上面的default-autowire="byName",如果没有这个声明那么HibernateDaoSupport中的sessionFactory不会被注入 --> <!-- 约定优于配置,约定优于配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingDirectoryLocations"> <list><!-- 这里直接映射的pojo类所在的包,简单方便不用没次加一个pojo类都需要到这里来添加 --> <value>classpath:com/fsj/spring/model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql"> true </prop> </props> </property> </bean> <!-- 自动扫描组件,这里要把web下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。--> <context:component-scan base-package="com.fsj.spring"> <context:exclude-filter type="regex" expression="com.fsj.spring.web.*"/> </context:component-scan> <!-- 下面是配置声明式事务管理的,个人感觉比用注解管理事务要简单方便 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <aop:config> <aop:advisor pointcut="execution(* com.fsj.spring.service.*Service.*(..))" advice-ref="txAdvice"/> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="query*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="load*" read-only="true"/> <tx:method name="*" rollback-for="Exception"/> </tx:attributes> </tx:advice> </beans>
spring3-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" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" default-autowire="byName"> <!-- 约定优于配置,约定优于配置 --> <!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd --> <mvc:resources mapping="/img/**" location="/img/"/> <mvc:resources mapping="/js/**" location="/js/"/> <mvc:resources mapping="/css/**" location="/css/"/> <!-- 扫描所有的controller --> <context:component-scan base-package="com.fsj.spring.web" /> <!-- InternalResourceViewResolver默认的就是JstlView所以这里就不用配置viewClass了 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 启用基于注解的处理器映射,添加拦截器,类级别的处理器映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <bean class="com.fsj.spring.util.MyHandlerInterceptor"/> </list> </property> </bean> <!-- 配置一个基于注解的定制的WebBindingInitializer,解决日期转换问题,方法级别的处理器映射, 有人说该bean要放在context:component-scan前面,要不然不起作用,但我试的放后面也可以啊。 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="cacheSeconds" value="0" /> <property name="webBindingInitializer"> <bean class="com.fsj.spring.util.MyWebBinding" /> </property> </bean> </beans>
log4j的就不贴出来了。
两个HelloWorldController如下:
package com.fsj.spring.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/*
* 不需要实现任何接口,也不需要继承任何的类
*/
@Controller
public class HelloWorldController {
/**
* 方法都可以接受的参数(参数数量和顺序没有限制): HttpServletRequest,HttpServletResponse,HttpSession(session必须是可用的) ,PrintWriter,Map,Model,@PathVariable(任意多个), @RequestParam(任意多个), @CookieValue (任意多个),@RequestHeader,Object(pojo对象) ,BindingResult等等
*
* 返回值可以是:String(视图名),void(用于直接response),ModelAndView,Map ,Model,任意其它任意类型的对象(默认放入model中,名称即类型的首字母改成小写),视图名默认是请求路径
*/
@RequestMapping("/helloWorld")
public ModelAndView helloWorld() {
ModelAndView mav = new ModelAndView();
mav.setViewName("login");
mav.addObject("message", "Hello World!");
return mav;
}
}
package com.fsj.spring.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.RequestParam;
import com.fsj.spring.model.TDept;
import com.fsj.spring.model.TUser;
import com.fsj.spring.service.IDeptService;
import com.fsj.spring.service.IUserService;
import com.fsj.spring.util.Constants;
@Controller
@RequestMapping("/user")
public class UserController {
private IUserService userService;
private IDeptService deptService;
public IDeptService getDeptService() {
return deptService;
}
public void setDeptService(IDeptService deptService) {
this.deptService = deptService;
}
public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
@RequestMapping(value="/login",method=RequestMethod.POST)
public String login(@RequestParam String name,@RequestParam String password,Model model,HttpServletRequest request) throws Exception{
TUser user1 = userService.getUserByName(name);
if(user1 == null) {
model.addAttribute("message", "用户不存在");
return "login";
}else if(password == null || !password.equals(user1.getPassword()) ){
model.addAttribute("message", "密码错误");
return "login";
}else {
request.getSession().setAttribute(Constants.USER_INFO_SESSION, user1);
return "welcome";
}
}
@RequestMapping(value="/login1",method=RequestMethod.POST)
public String login1(TUser user,HttpServletRequest request,Model model) throws Exception{
TUser user1 = userService.getUserByName(user.getName());
if(user1 == null) {
model.addAttribute("message", "用户不存在");
return "login";
}else if(user.getPassword() == null || !user.getPassword().equals(user1.getPassword()) ){
model.addAttribute("message", "密码错误");
return "login";
}else {
request.getSession().setAttribute(Constants.USER_INFO_SESSION, user1);
return "welcome";
}
}
@RequestMapping(value="/list")
public String list(Model model,HttpServletRequest request) throws Exception {
List<TUser> userList = userService.getUserList();
model.addAttribute("userList", userList);
List<TDept> deptList = deptService.getDeptList();
model.addAttribute("deptList", deptList);
if(StringUtils.isNotBlank(request.getParameter("resMess")) && StringUtils.isNotBlank(request.getParameter("opeMess"))) {
model.addAttribute("message",setOperateMessage(request.getParameter("resMess"),request.getParameter("opeMess"),"用户"));
}
return "user/list";
}
private String setOperateMessage(String resMess,String opeMess,String modMess) {
//TODO 以后可以和写日志结合在一起
String ope = "";
String res = "";
if(Constants.OPERATE_TYPE_ADD.equals(opeMess)) {
ope = "增加";
}else if(Constants.OPERATE_TYPE_UPDATE.equals(opeMess)) {
ope = "更新";
}else if(Constants.OPERATE_TYPE_DELETE.equals(opeMess)) {
ope = "删除";
}
if(Constants.RESULT_SUCCESS.equals(resMess)) {
res = "成功";
}else if(Constants.RESULT_FAILED.equals(resMess)) {
res = "失败";
}
return ope + modMess + res;
}
/*
* 同样的请求路径 user/add 如果是get请求就转到增加页面去,如果是post请求就做add操作
*/
@RequestMapping(value="/add",method=RequestMethod.GET)
public String toAdd(Model model) throws Exception{
List<TDept> deptList = deptService.getDeptList();
model.addAttribute("deptList", deptList);
return "user/add";
}
@RequestMapping(value="/add",method=RequestMethod.POST)
public String doAdd(TUser user,Model model) throws Exception{
try {
userService.addUser(user);
model.addAttribute("resMess", Constants.RESULT_SUCCESS);
} catch (Exception e) {
e.printStackTrace();
model.addAttribute("resMess", Constants.RESULT_FAILED);
throw e;
}
model.addAttribute("opeMess", Constants.OPERATE_TYPE_ADD);
//重定向,防止重复提交,当然这样不能完全解决重复提交的问题,只是简单处理一下,若要较好的防止重复提交可以结合token做,
//以“/”开关,相对于当前项目根路径,不以“/”开关,相对于当前路径
//return "redirect:/user/list";
return "redirect:list";
}
/*
* Restful模式路径:
* 注意这里/update/{id}和@PathVariable("id")中id要一致,这样不管用debug模式还是release模式编译都没问题
* 也可以简写成@PathVariable int id,但这样只能以debug模式编译的时候正确,如果用release编译就不正确了,因为如果用release模式编译会把参数的名字改变的
* 一般IDE工具都是以debug模式编译的,javac是以release模式编译的
* 同样的请求路径 user/update 如果是get请求就转到增加页面去,如果是post请求就做update操作
*/
@RequestMapping(value="/update/{id}",method=RequestMethod.GET)
public String toUpdate(@PathVariable("id") int id, Model model) throws Exception{
model.addAttribute("user",userService.getUserById(id));
model.addAttribute("deptList", deptService.getDeptList());
return "user/update";
}
@RequestMapping(value="/update/{id}",method=RequestMethod.POST)
public String doUpdate(@PathVariable("id") int id, TUser user,Model model) throws Exception{
try {
userService.updateUser(user);
model.addAttribute("resMess", Constants.RESULT_SUCCESS);
} catch (Exception e) {
e.printStackTrace();
model.addAttribute("resMess", Constants.RESULT_FAILED);
throw e;
}
model.addAttribute("opeMess", Constants.OPERATE_TYPE_UPDATE);
//return "redirect:../list";
//重定向,防止重复提交,以“/”开关,相对于当前项目根路径,不以“/”开关,相对于当前路径
return "redirect:/user/list";
}
@RequestMapping(value="/delete/{id}")
public String delete(@PathVariable("id") int id,Model model)throws Exception{
try {
userService.deleteUser(id);
model.addAttribute("resMess", Constants.RESULT_SUCCESS);
} catch (Exception e) {
e.printStackTrace();
model.addAttribute("resMess", Constants.RESULT_FAILED);
throw e;
}
model.addAttribute("opeMess", Constants.OPERATE_TYPE_DELETE);
return "redirect:/user/list";//重定向
}
}
下面的例子中没有jar包,jar太大了超过10M了,请自己加jar包