为什么 ModelMap 属性的值在 jsp 中发生变化?

发布于 2024-12-22 19:41:56 字数 3870 浏览 1 评论 0原文

我有一个控制器如下:

@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>

为什么当我设置/更改命令对象 userDetailsCmd 的值时 user.userType 的值总是发生变化(userDetailsCmd .userType)在jsp中? 这是因为两者具有相同的属性名称(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...
}

而 User 类(POJO)是:

public class User{

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

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

   }

i have a controller given below:

@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 is:

<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>

why value of user.userType always got changed when i set/change value of command object userDetailsCmd (userDetailsCmd.userType) in jsp?
is this because both have same attribute name (userType)?

EDIT:
UserCommand class (command object)is :

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...
}

And User class (a POJO )is:

public class User{

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

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

   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月如刀 2024-12-29 19:41:56
@InitBinder("user")
public void userBinding(DataBinder binder) {
    binder.setDisallowedFields("userType");
}

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

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

Add this to your controller and the userType field of the User object will not get populated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文