ITeye博客

Spring MVC 3.x annotated controller的几点心得体会

itstarting · 2010-11-29 17:59 · Java框架 · 企业架构 · Markdown

最近拿Spring MVC 3.x做项目,用了最新的系列相关Annotation来做Controller,有几点心得体会值得分享。

 

转载请注明 :IT进行时(zhengxianquan AT hotmail.com) from http://itstarting.iteye.com/

 

一、编写一个AbstractController.java,所有的Controller必须扩展

    除了获得Template Design Pattern的好处之外,还能一点,那就是放置全局的@InitBinder:

public class AbstractController {
...
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat, false));
	}
...
}

二、每个域都应该有一个Controller,且做好URI规划

    大家都知道Spring MVC 3.x是完全支持Restful的,我们把URI做好规划,对于诸如ACL的实现会有很大的帮助。建议的URI规划如下:{Domain}[/{SubDomain}]/{BusinessAction}/{ID}。比如:

    hotels/bookings/cancel/{id} ——表示此URI匹配hotels域的bookings子域,将要进行的是取消某项booking的操作。代码如下:

@Controller
@RequestMapping(value = "/hotels")
public class HotelsController extends AbstractController {
...
	@RequestMapping(value = "/bookings/cancel/{id}", method = RequestMethod.POST)
	public String deleteBooking(@PathVariable long id) {
		bookingService.cancelBooking(id);
		//use prefix 'redirect' to avoid duplicate submission
		return "redirect:../../search";
	}
...
}

 

 

    另外还有几个重要原因:

    1、由于Spring的DefaultAnnotationHandlerMapping.java在做Mapping的时候,先做是否有类匹配,再找方法,把基本的mapping放在类上面,可以加速匹配效率;

    2、后续可以通过更细腻的支持Ant path style的AntPathMatcher来规划URI Template资源,做ACL控制(请参考后面的心得体会)。

三、JSON/XML等ajax的支持很cool,可以尝试

    JSON/XML/RSS等均可支持,当然有些denpendency,比如JSON的默认支持,需要jackson jar出现在lib中,POM的artifact如下:

<dependency>
	<groupId>org.codehaus.jackson</groupId>
	<artifactId>jackson-mapper-asl</artifactId>
	<version>1.5.3</version>
</dependency>

   这样,我们其实根本就不需要进行额外的JSON转换了,Spring MVC 3会根据请求的格式自行转换:

	@ResponseBody
	@RequestMapping(value = "/ajax", method = RequestMethod.POST)
	public JsonDataWrapper<Hotel> ajax(WebRequest request, Hotel hotel, Model model) 
		throws Exception {
		JsonDataWrapper<Hotel> jsonDataWrapper = this.getPaginatedGridData(request, hotel, hotelService);
		return jsonDataWrapper;
	}

   注 :我上面的JsonDataWrapper只是我自己做的一个简单的wrapper,目的是为jQuery Flexigrid plugin做数据准备的。还是贴出来吧:

/**
 * A wrapper class for jQuery Flexigrid plugin component.
 * 
 * The format must be like this:
 * <code>
 * 		{"total":2,"page":1,"rows":[
 * 			{"personTitle":"Mr","partyName":"Test8325","personDateOfBirth":"1970-07-12"},
 * 			{"personTitle":"Ms","partyName":"Ms Susan Jones","personDateOfBirth":"1955-11-27"}
 * 		]}
 * </code>
 * 
 * @author bright_zheng
 *
 * @param <T>: the generic type of the specific domain
 */
public class JsonDataWrapper<T> implements Serializable {
	private static final long serialVersionUID = -538629307783721872L;

	public JsonDataWrapper(int total, int page, List<T> rows){
		this.total = total;
		this.page = page;
		this.rows = rows;
	}
	private int total;
	private int page;
	private List<T> rows;
	
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public List<T> getRows() {
		return rows;
	}
	public void setRows(List<T> rows) {
		this.rows = rows;
	}
}

四、Controller的单元测试变得很可为且简单

    以前的项目从来不做controller层的测试,用了Spring MVC 3这一点就不再难为情,做吧:

public class HotelsControllerTest {
	
	private static HandlerMapping handlerMapping;
	private static HandlerAdapter handlerAdapter;
	
	private static MockServletContext msc;

	@BeforeClass
	public static void setUp() {
		String[] configs = {
				"file:src/main/resources/context-*.xml",
				"file:src/main/webapp/WEB-INF/webapp-servlet.xml" };
		XmlWebApplicationContext context = new XmlWebApplicationContext();  
        context.setConfigLocations(configs);  
        msc = new MockServletContext();  
        context.setServletContext(msc);  
        context.refresh();  
        msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
        ApplicationContextManager manager = new ApplicationContextManager();
		manager.setApplicationContext(context);		
		handlerMapping = (HandlerMapping) ApplicationContextManager.getContext().getBean(DefaultAnnotationHandlerMapping.class);
		handlerAdapter = (HandlerAdapter) ApplicationContextManager.getContext().getBean(ApplicationContextManager.getContext().getBeanNamesForType(AnnotationMethodHandlerAdapter.class)[0]);		
	}

	@Test
	public void list() throws Exception {
		MockHttpServletRequest request = new MockHttpServletRequest();
		MockHttpServletResponse response = new MockHttpServletResponse();
		
		request.setRequestURI("/hotels");
		request.addParameter("booking.id", "1002");
		request.addParameter("hotel.name", "");
		request.setMethod("POST");
		
		//HandlerMapping
		HandlerExecutionChain chain = handlerMapping.getHandler(request);
		Assert.assertEquals(true, chain.getHandler() instanceof HotelsController);
		
		//HandlerAdapter
		final ModelAndView mav = handlerAdapter.handle(request, response, chain.getHandler());
		
		//Assert logic
		Assert.assertEquals("hotels/search", mav.getViewName());
	}
}

    需要说明一下 :由于当前最想版本的Spring(Test) 3.0.5还不支持@ContextConfiguration的注解式context file注入,所以还需要写个setUp处理下,否则类似于Tiles的加载过程会有错误,因为没有ServletContext。3.1的版本应该有更好的解决方案,参见:https://jira.springsource.org/browse/SPR-5243 。

    Service等其他layer就没有这类问题,测试的写法将变得更加优雅,贴个出来:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/resources/context-*.xml" })
public class DefaultServiceImplTest {
	/** @Autowired works if we put @ContextConfiguration at junit type */
	@Autowired
	@Qualifier("hotelService")
	private HotelService<Hotel> hotelService;

	@Test
	public void insert() {
		Hotel hotel = new Hotel();
		hotel.setAddress("addr");
		hotel.setCity("Singapore");
		hotel.setCountry("Singapore");
		hotel.setName("Great Hotel");
		hotel.setPrice(new BigDecimal(200));
		hotel.setState("Harbarfront");
		hotel.setZip("010024");
		hotelService.insert(hotel);
	}
}

 

五、ACL可以写一个HandlerInterceptorAdapter,在“事前”拦截。

    由于Spring是Restful friendly的framework,做ACL就不要用过滤器了,用interceptor吧:

public class AclInterceptor extends HandlerInterceptorAdapter {
	private static final Logger logger = Logger.getLogger(AclInterceptor.class);
	
	/** default servlet prefix */
	private static final String DEFAULT_SERVLET_PREFIX = "/servlet";
	
	/** will be injected from context configuration file */
	private AclService service;
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String currentUri = request.getRequestURI();
		boolean isAccessible = true;
		//only intercept for annotated business controllers
		Controller c = AnnotationUtils.findAnnotation(handler.getClass(), Controller.class);
		if(c!=null){
			String[] grantedResource = getGrantedResource(request);
			if(grantedResource==null || grantedResource.length==0){
				throw new AccessDeniedException("No resource granted");
			}
			isAccessible = service.isAccessible(grantedResource, currentUri);
			if(logger.isDebugEnabled()){
				logger.debug("ACL interceptor excueted. Accessible for Uri[" + currentUri +"] = " + isAccessible);
			}
			//if isAccessible==true, throw custom AccessDeniedException
			if(!isAccessible) throw new AccessDeniedException();
		}
		return isAccessible;
	}
	
	/**
	 * transfer the original Uri to resource Uri
	 * e.g.:
	 * 	original Uri: /servlet/hotels/ajax
	 * 	target Uri  : /hotels/ajax
	 * @param originalUri
	 * @return
	 */
	protected String getUri(String originalUri){
		return originalUri.substring(DEFAULT_SERVLET_PREFIX.length());
	}
	
	/**
	 * Get the granted resource from session
	 * @param request
	 * @return
	 */
	protected String[] getGrantedResource(HttpServletRequest request){
		//get the resources from current session
		//String[] uriResourcePattern = (String[]) request.getSession().getAttribute("uriResourcePattern");		
		//TODO: mock data here
		String[] uriResourcePattern = new String[]{"/**"};
		
		return uriResourcePattern;
	}

	public void setService(AclService service) {
		this.service = service;
	}
}

    注 :上面还有TODO部分,很简单,登录后把当然用户可访问的Ant path style的URI Resources放到session中,作为一个String[],这里拿来出匹配即可,建议匹配就用org.springframework.util.AntPathMatcher。

 

六、对于Ajax Form Validation,正在寻求更佳的解禁方案。

    Spring 3已经支持@Valid的注解式validation,又一个JSR,但通过我研究代码发现,默认的org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.java实现还是非常封闭,并不能很好的实现binding后执行业务代码前处理,所以目前的实现办法还不算优雅。

    已经实现,大致的思路是:

    1)发出ajax form post请求(用jQuery);

    2)正常mapping到controller的方法中处理;

    3)做form validation;

    4)不管怎样,forward到一个解析错误消息的JSP,叫ajaxFormValidatorResult.jsp;

    5)再写一个jquery plugin,叫errorBinder.js,解析错误并自定binding为Tooltip,如果没错误就正常submit

贴个简单的demo效果,欢迎拍砖。

  ==Before


  ==After

 

 

补充说明:后面陆续补充的心得,就不在这里出现了,而是以跟帖的形式存在,便于讨论吧

评论(80)

  1. #80 楼 · itstarting · 2011-05-30 09:51

    jason&xu writes:

    感觉SPRING MVC的数据绑定比较差,没struts2方便。

    楼上可否举例一二加以说明?

    据我所知,spring mvc的form binding和validation都还是不错的了

    其中的form binding只要加上一个model,即可完全面向pojo对象实现form <-> controller param之间的绑定与传递

    struts2一直没兴趣看,也就不得而知其高明之处了,还请指教

  2. #79 楼 · jason&xu · 2011-05-29 23:39

    感觉SPRING MVC的数据绑定比较差,没struts2方便。

  3. #78 楼 · itstarting · 2011-05-29 21:09

    kjj writes:

    虽然没用springmvc但是最后还是我一票,定为精华了,楼主的确认认真,建议分片,分类整理成博客,便于后来人学习!!!

    哈哈,谢谢了,我还一直奇怪怎样才能捞个精华呢

    不过也说明spring mvc还是有其可取之处,哪怕相比struts

  4. #77 楼 · kjj · 2011-05-29 14:17

    虽然没用springmvc但是最后还是我一票,定为精华了,楼主的确认认真,建议分片,分类整理成博客,便于后来人学习!!!

  5. #76 楼 · itstarting · 2011-05-26 15:37
  6. #75 楼 · itstarting · 2011-05-26 15:36
  7. #74 楼 · suyulin6688 · 2011-05-26 14:49
  8. #73 楼 · itstarting · 2011-05-25 22:26
    &lt;!-- Map all /resources requests to the Resource Servlet for handling --&gt;
    &lt;servlet-mapping&gt;
    	&lt;servlet-name&gt;Resources Servlet&lt;/servlet-name&gt;
    	&lt;url-pattern&gt;/resources/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    
    &lt;!-- The front controller of this Spring Web application, responsible for handling all application requests --&gt;
    &lt;servlet&gt;
    	&lt;servlet-name&gt;Controller&lt;/servlet-name&gt;
    	&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
    	&lt;init-param&gt;
    		&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
    		&lt;param-value&gt;
    			/WEB-INF/webapp-servlet.xml
    		&lt;/param-value&gt;
    	&lt;/init-param&gt;
    	&lt;load-on-startup&gt;2&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    	
    &lt;!-- Map all requests to the DispatcherServlet for handling --&gt;
    &lt;servlet-mapping&gt;
    	&lt;servlet-name&gt;Controller&lt;/servlet-name&gt;
    	&lt;url-pattern&gt;/servlet/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;</pre>
    

     

  9. #72 楼 · rocketball · 2011-05-25 14:47
  10. #71 楼 · yinyanbin · 2011-05-25 13:45
  11. #70 楼 · rocketball · 2011-05-25 12:49
  12. #69 楼 · yinyanbin · 2011-05-25 12:02
  13. #68 楼 · itstarting · 2011-05-19 17:03

    pchg119 writes:

    我不是来灌水的 只是看到这个目前网络流行的一塌糊涂的 springmvc就喜欢看看 做商务网站是用spring好还是不用好呢?

    商务网站要考虑的东西更多,比如横向/纵向扩展、缓存、可用性、可维护性……注入此类,简单一句用不用spring解决不了问题

    用spring更多是考虑研发的绩效问题,至于“商务网站”,综合考虑再说吧,感觉与spring没太直接关系

  14. #67 楼 · pchg119 · 2011-05-19 13:43

    我不是来灌水的 只是看到这个目前网络流行的一塌糊涂的 springmvc就喜欢看看 做商务网站是用spring好还是不用好呢?

  15. #66 楼 · haitaohehe · 2011-05-17 16:12

    楼主能否提供下spring mvc本身验证的例子,对于jsr303个人感觉倾入性太大,官方的例子好像就是基于jsr303的...

  16. #65 楼 · yin_bp · 2011-05-06 14:18

    大家讨论了这么多收益匪浅,不过还是建议大家看看bbossgroups项目,呵呵,有demo,有eclipse工程,丰富的数据绑定参数机制,没有这么多的注解,学习成本低,容易上手,能够解决实际问题,呵呵,spring mvc没法比,详情请看官方博客: http://yin-bp.iteye.com/

  17. #64 楼 · itstarting · 2011-03-23 15:03

    muqingren writes:

    楼主请教 [code=java]@SessionAttributes(types=UserInfoVo.class,value="userinfo")[/code] 我用这个注解后,运行提示我: [code=java]org.springframework.web.HttpSessionRequiredException: Session attribute 'userinfo' required - not found in session [/code]

    这是为什么??谢谢

    提示很清楚呀:你需要的属性为userinfo的session对象不存在

    是否有optional=true之类的属性可以用用,试一下咯

  18. #63 楼 · muqingren · 2011-03-18 17:09

    楼主请教

    @SessionAttributes(types=UserInfoVo.class,value="userinfo")
    

    我用这个注解后,运行提示我:

    org.springframework.web.HttpSessionRequiredException: Session attribute 'userinfo' required - not found in session
    
    

    这是为什么??谢谢

  19. #62 楼 · itstarting · 2011-03-18 14:51

    自学得1)搞清楚自己要什么;2)找对资料;3)可以应用并持续解决问题

  20. #61 楼 · njbble · 2011-03-17 16:04

    kongruxi writes:

    楼主写得不错啊,我也正在学spring MVC 3 不过,我看了一下spring 3 API,发现很多Controler都变为Deprecated,建议用注解代替。但不清楚AbstractWizardFormController这个怎样用注解来代替?请教楼主了……

    是啊,自学API看的有点头大

评论功能维护中,当前仅展示历史评论。