2013-08-31 59 views
0

我是Spring MVC的新手。我正在写一个小应用程序来学习。我正在研究一些基本的CRUD。我多次编写相同的代码来编辑/显示域类。什么是更好/合适的方式来实现编辑和保存方法?CRUD with Spring MVC - DRY建议

感谢

控制器:

@RequestMapping(value="/userInfo/create/{id}") 
public ModelAndView edit(@PathVariable Integer id, Model model) 
{ 
    logger.info("UserInfo edit {}", id); 

    UserInfo userInfo = userInfoService.get(id); 

    model.addAttribute("userInfo", userInfo); 
    model.addAttribute("parent" , userInfoService.get(userInfo.getParentId())); 
    model.addAttribute("allUsers", userInfoService.list()); 
    model.addAttribute("affInfos", affInfoService.findAllByUserInfo(userInfo)); 

    ModelAndView mv = new ModelAndView("userInfo/create", "command", model); 

    return mv; 
} 


@RequestMapping(value="/userInfo/save", method=RequestMethod.POST) 
public ModelAndView save(@Valid @ModelAttribute("userInfo")UserInfo userInfo, BindingResult result, Model model) 
{ 
    logger.info("UserInfo save"); 

    model.addAttribute("userInfo", userInfo); 
    model.addAttribute("parent" , userInfoService.get(userInfo.getParentId())); 
    model.addAttribute("allUsers", userInfoService.list()); 
    model.addAttribute("affInfos", affInfoService.findAllByUserInfo(userInfo)); 

    ModelAndView mv = new ModelAndView("userInfo/create", "command", model); 

    if(!result.hasErrors()) 
    { 
     userInfoService.saveOrUpdate(userInfo); 
     model.addAttribute("flashMsg", "UserInfo saved!"); 
    } 
    else 
    { 
     model.addAttribute("flashMsg", "Could not save."); 
    } 

    return mv; 
} 

的观点:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 

<html> 
<head> 
    <title>Create/Edit UserInfo</title> 
</head> 
<body> 

<div style="margin-left:100px;font-weight: bold;font-size:16px;margin-bottom:20px;margin-top: 10px;">User Information</div> 

<c:if test="${flashMsg != null}"> 
    <div>${flashMsg }</div> 
</c:if> 

<form:form modelAttribute="userInfo" action="${pageContext.request.contextPath}/userInfo/save" method="post" > 

<form:hidden path="id"/> 

<div style="width:200px;margin-left:100px;margin-bottom:10px;"> 
    <div style="margin-bottom:4px;font-weight: bold;">Name</div> 
    <div><form:input path="name"/></div> 
</div> 

<div style="width:200px;margin-left:100px;margin-bottom:10px;"> 
    <div style="margin-bottom:4px;font-weight: bold;">Parent</div> 
    <div> 
     <form:select path="parentId" itemLabel="name" itemValue="id" > 
      <form:option value="-1">Choose Parent</form:option> 
      <form:options items="${allUsers}" itemLabel="name" itemValue="id"/> 
     </form:select> 
    </div> 
</div> 

<c:if test="${affInfos != null }"> 
<div> 
    <table style="width:600px;border:1px solid #ccc;" class="center ui-corner-all shadow zebra-striped"> 
     <thead> 
      <tr> 
       <th>Macro</th> 
       <th>AffID</th> 
       <th><button type="button" class="btn shadow">New</button></th> 
      </tr> 
     </thead> 
     <tbody> 
      <c:forEach var="affInfo" varStatus="i" items="${affInfos }"> 
       <tr> 
        <td><input type="text" name="macro_${affInfo.id}" id="macro_${affInfo.id}" value="${affInfo.macro}"></td> 
        <td><input type="text" name="affid_${affInfo.id}" id="affid_${affInfo.id}" value="${affInfo.affid}"></td> 
        <td><button class="btn shadow" type="button">Delete</button></td> 
       </tr> 
      </c:forEach> 
     </tbody> 
    </table> 
</div> 
</c:if> 

<div style="margin-left:100px;margin-top:10px;" > 
    <form:button class="btn shadow">Submit</form:button> 
</div> 

</form:form> 


</body> 
</html> 

回答

2

首先,你当然可以创建这样一个方法:

private method init(){ 
    model.addAttribute("userInfo", userInfo); 
    model.addAttribute("parent" , userInfoService.get(userInfo.getParentId())); 
    model.addAttribute("allUsers", userInfoService.list()); 
    model.addAttribute("affInfos", affInfoService.findAllByUserInfo(userInfo)); 
} 

但是,如果你控制LER仅用于一个JSP页面(JSPX是推荐页面类型),你可以使用类似的东西对你的JSP页面中的每个属性:

@ModelAttribute("allUsers") 
public List<User> populateUserInfoList() { 
    return userInfoService.list(); 
} 

它会自动添加到ModelAndView的属性至极的名字是在@ModelAttribute注释中。但要小心,每次使用控制器时都会调用它,如果您的控制器不仅仅是调用总是需要相同数据的相同类型的JSP,则可能会创建对数据库无用的调用。

有了这个,你不再需要这一行:

model.addAttribute("allUsers", userInfoService.list()); 

希望它可以帮助你

+0

谢谢,这让我在正确的方向前进。 – Jason