使用spring和hibernate更新数据库中的用户表单字段时如何避免空字符串

时间:2012-03-23 08:06:43

标签: java spring spring-mvc

我刚开始使用hibernate.after用户登录我需要更新用户名,密码,emailId,当用户想要更改这些字段时。但是这里用户可以更改任何字段,例如他可以更改emailId并且他留下了密码和用户名空字符串...但是在服务中我想只更新他在数据库中给出值的字段,如果他留下任何字段空字符串我想避免该字段(但是它正在更新空字符串)数据库)。我不确定他想要改变哪个领域,即所有领域或任何领域。任何人都可以建议我如何满足这一要求。提前谢谢。

这是jsp文件:

  <%
  String path=request.getContextPath();
  %>
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">`enter code here`
   <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 <title>Untitled Document</title>
  <link href="<%=path %>/css/style.css" rel="stylesheet" type="text/css" />
 </head>

   <body>
  <form action="modifyUser.htm" method="get">
   <table width="100%" border="0" align="left" cellpadding="0" cellspacing="0" 
  class="fields">
 `enter code here`<tr>
   <td width="33%">User Name <span class="star"></span></td>
  <td width="67%"><input name="userName" type="text" /></td>
  </tr>
  <tr>
   <td>Password</td>
   <td><input name="password" type="password" /></td>
  </tr>
  <tr>
  <td height="35">Email ID </td>
  <td><input name="emailId" type="text" /></td>
  </tr>
  <tr>
  <td>&nbsp;</td>
   <td align="left"><input type="image" src="<%=path %>/images/update.png" height="2 5" 
        `width="65" /></td>
     </tr>
    </table>
       </form>
       </body>
         </html>

这里是主控制器:

public class MainController {

    @RequestMapping("/modifyUser.htm")
    public ModelAndView updateUser(@RequestParam String userName,@RequestParam String password,@RequestParam String emailId,HttpSession session)throws Exception{
        User user=null;
        try{
            user=(User)session.getAttribute("userSession");
            if(userName.equalsIgnoreCase(currentUserName)){
                System.out.println("i'm in same username case");
                user.setUserPassword(password);
                user.setUserEmailId(emailId);
                userService.updateUser(user);
                return new ModelAndView("user_management","message","user_modified");
            }else if(userService.isUserNameValid(userName)){    
                System.out.println("here username also changing 2nd case");
                user.setUserName(userName);
                user.setUserPassword(password);
                user.setUserEmailId(emailId);
                userService.updateUser(user);
                return new ModelAndView("user_management","message","user_modified");
            }else {
                System.out.println("case failed to update user");
                return new ModelAndView("external2","message","invalid_username");
            }
        }catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
            logger.error("in updating a user with username:"+userName+" ",e);
            return new ModelAndView("error","message",e);
            // TODO: handle exception
        }
    }
}

3 个答案:

答案 0 :(得分:0)

有更优雅的方式来做你正在做的事情(比如使用spring的form元素:http://static.springsource.org/spring/docs/3.0.x/reference/htmlsingle/#view-jsp-formtaglib

但是对于你发布的这段代码,你可以这样做:

if (org.springframework.util.StringUtils.hasText(password)){
    user.setUserPassword(password);
}

这同样适用于其他领域。

答案 1 :(得分:0)

只需加载用户从数据库中省略的字段即可。然后,您可以再次存储整个(ORM映射的)域对象。

答案 2 :(得分:0)

你可以使用spring的内置验证器界面。

http://static.springsource.org/spring/docs/2.5.x/reference/validation.html