为什么在jsp中改变ModelMap属性的值?

时间:2011-12-27 15:45:36

标签: spring spring-mvc controller modelattribute

我有一个控制器如下:

@Controller
@SessionAttributes("user")
public class UserController {

@Autowired
AdminDaoInterface adminDao; 

@Autowired
UserValidator userValidator;

@RequestMapping(value="userdetails.htm",method=RequestMethod.GET)
public String userDetails(ModelMap model) {
  UserCommand userDetailsCmd = new UserCommand();
  model.addAttribute("userDetailsCmd", userDetailsCmd);
  return "UserDetails";  // will go to UserDetails.jsp
}


@RequestMapping(value="userdetails.htm",method = RequestMethod.POST) 
public String userDetailsSubmit(
                      @ModelAttribute("userDetailsCmd") UserCommand userDetailsCmd,
                      Errors errors, ModelMap model){

  //user will contains details of user found from database.
  User user=adminDao.getUserOnId(userDetailsCmd.getUserId());

  if(user==null) {  
    errors.rejectValue("userId", "user.not.exists");
    return "UserDetails";
  }

  model.addAttribute("user",user);
  return "ChangeUserDetails"; // will go to ChangeUserDetails.jsp
}


@RequestMapping(value="changeuserdetails.htm",method = RequestMethod.POST) 
public String changeUserDetails(
             @ModelAttribute("userDetailsCmd") UserCommand userDetailsCmd,
             @ModelAttribute("user") User user){

  // some stuff...

  // BOTH user and userDetailsCmd OBJECTS HAVE A FIELD "userType"
  // HERE  user.userType SHOULD CONTAIN VALUE COLLECTED FROM DB (already
      // existing value)

  // AND userDetailsCmd.userType SHOULD CONTAIN VALUE SELECTED IN JSP IN 
  //ORDER TO UPDATE EXISTING VALUE of user.userType.                
  ...

  //BUT WHEN I SET VALUE OF userDetailsCmd.userType IN JSP, WHY VALUE OF 
  // user.userType GOT CHANGED ALWAYS TO THAT VALUE???
  ...

  // somewhere later in code i m setting user.userType to
  //userDetailsCmd.userType to update value.

  adminDao.updateUser(user);
  return "ChangeUserSuccess"; 
}

 }

ChangeUserDetails.jsp是:

<form:form method="post" action="changeuserdetails.htm" commandName="userDetailsCmd"> 

<table>

<tr>
    <td>User Id :</td>
    <td>${user.userId}</td>

</tr>


<tr>
    <td>Project :</td>
    <td>${user.projectId} </td>
    <td>Change To:</td>
    <td>
    <form:select path="projectId"> 
    <form:option value="-1" label="SELECT"/>
    <form:options items="${projectList}" itemValue="pid"        
                 itemLabel="projectName"/>
    </form:select>
    </td>   
    </tr>
<tr>
    <td>User Type :</td>
    <td>${user.userType}</td>
    <td>Change To:</td>
    <td>
    <form:select path="userType">
        <form:option value="not changed" label="SELECT"/>
        <form:option  value="admin" label="Admin" />
        <form:option value="user" label="User" />
    </form:select>
    </td>   
</tr>

<tr>
    <td colspan="2">
        <input type="submit" value="Change User Details"/>
    </td>
</tr>
</table> 

</form:form>

为什么在jsp中设置/更改命令对象 userDetailsCmd userDetailsCmd.userType )的值时, user.userType 的值总是会更改? 这是因为两者都有相同的属性名称(userType)?

修改 UserCommand类(命令对象)是:

public class UserCommand{

private String userId;
private String password;
private int projectId;
private String userType;
private int listUserId;
private List usersList;

//PLUS getters and setters for these fields...
}

用户类(POJO)是:

public class User{

private String userId;
private String password;
private int projectId;
private String userType;

//PLUS getters and setters for these fields...

   }

1 个答案:

答案 0 :(得分:0)

@InitBinder("user")
public void userBinding(DataBinder binder) {
    binder.setDisallowedFields("userType");
}

将此添加到您的控制器,userType对象的User字段将不会被填充。