【问题标题】:Spring MVC keep value of unexposed property modelSpring MVC 保留未公开属性模型的值
【发布时间】:2013-05-08 22:53:59
【问题描述】:

我有一个模型类:

public class User{
    public String title;
    public String username;

    /* setter getter here */

    public String toString(){
        /* output title and username */
    }
}

控制器

@RequestMapping("/dummy")
public class DummyController
{
    private log = Logger.getLogger(this.getClass().getName());

    @RequestMapping(value="binder")
    public String binderInput(Model model){ 
        UserLogin userInit=UserLogin.findUserLogin(1L);
        model.addAttribute("userLogin",userInit);               
        return "binderInput";   
    }

    @RequestMapping(value="binderResult")
    public String binderResult(@ModelAttribute("userLogin") UserLogin userLogin,BindingResult br){
        log.debug("userLogin:"+userLogin);
        return "binderResult";  
    }
}

views (jspx) 用于 binderInput:

<form:form action="dummy/binderResult" method="POST" modelAttribute="userLogin">
    title:<form:input path="title" />
    <input type="submit"/>
</form:form>

DAO UserLogin.findUserLogin(1L); 将返回标题为“Mr”和用户名“Smith”的用户。 我浏览localhost:8080/myWeb/dummy,视图在标题字段中显示“先生”,然后我将标题更新为“先生”并提交表单。

我希望在 binderResult 中看到

title:Sir, username:Smith

但实际上我得到了

title:Sir, username:null

有没有办法保留 DAO 中未编辑的属性值(对于未在 jspx 视图中显示的字段)?

  1. 我想到了隐藏字段,但是在查看html源代码时可以查看用户名并且可以对其进行操作,所以我认为它不能成为解决方案。

  2. 我尝试过@SessionAttribute,我认为将模型保留在会话中是有意义的,然后spring modelAttribute 只会填充从视图接收到的属性到会话模型。但是还是一样,标题变成了Sir但是用户名是空的。

  3. 我阅读了有关 @InitBinder 的信息并尝试了
    dataBinder.setAllowedFields(new String[]{"title"});
    但它没有帮助,它只是过滤属性而不保留其他属性的值不允许字段

我开始认为我必须手动执行以下操作:

    @RequestMapping(value="binderResult")
    public String binderResult(@ModelAttribute("userLogin") UserLogin userLogin,BindingResult br,HttpSession session){
        /* after put DAO model in session in binderInput */
        UserLogin userLoginSession = session.getAttribute("userLoginSession");
        userLoginSession.setUsername(userLogin.getUsername());
                    //further process or save userLoginSession
        log.debug("userLogin:"+userLoginSession);
        return "dummy/binderResult";    
    }


有没有办法像我最初期望的那样在 Spring 中保留未编辑的属性?

感谢您的帮助!

================================================ ======

gouki 提出的第二点代码:

@RequestMapping("/dummy")
@SessionAttributes({"userSession"})
public class Dummy2Controller
{
    private log = Logger.getLogger(this.getClass().getName());

    @RequestMapping(value="binder")
    public String binderInput(Model model,HttpSession session){ 
        UserLogin userInit=UserLogin.findUserLogin(1L);
        model.addAttribute("userSession",userInit);             
        return "binderInput";   
    }

    @RequestMapping(value="binderResult")
    public String binderResult(@ModelAttribute UserLogin userSession,BindingResult br,HttpSession session){
        log.debug("userSession:"+userSession);
        //further process or save userSession to DB
        return "binderResult";  
    }
}

并在视图中将 modelAttribute 更改为 userSession

&lt;form:form action="dummy/binderResult" method="POST" modelAttribute="userSession"&gt;

================================================ ======
问题已解决
我阅读的一些链接与此问题中已接受的答案相关:

  1. springsource docs
  2. stackoverflow explanation by @Christopher Yang
  3. Captain Debug's Blog

【问题讨论】:

  • 请显示您的第二个解决方案的代码。
  • 感谢回复,我已经更新了问题

标签: spring spring-mvc modelattribute


【解决方案1】:

您可以添加方法,该方法将在调用每个控制器方法之前填充对象:

@ModelAttribute("userLogin")
public UserLogin getUserLogin() {
    return UserLogin.findUserLogin(1L);
}

从提交的表单加载更改后的字段将覆盖旧值,并且在您的控制器中您将刷新对象。

【讨论】:

  • 谢谢你的作品!只是不要忘记确保在您的 modelAttribute 参数之后有 BindingResult,否则它不起作用(CMIIW)。我将更新问题以添加一些可能对此有所帮助的链接,称为 ModelAttribute on a Method
【解决方案2】:

与您的问题没有直接关系,我可能会先使用 Max 的方法,但您可以使用 Spring Webflow 及其对话上下文来保留对象的副本:

http://static.springsource.org/spring-webflow/docs/2.0.x/reference/htmlsingle/spring-webflow-reference.html

搜索 conversationScope,他们不提供直接指向它的链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 2020-12-11
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    相关资源
    最近更新 更多