Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc Spring MVC 3命令对象值在GET和POST操作之间丢失_Spring Mvc - Fatal编程技术网

Spring mvc Spring MVC 3命令对象值在GET和POST操作之间丢失

Spring mvc Spring MVC 3命令对象值在GET和POST操作之间丢失,spring-mvc,Spring Mvc,我的域对象不会在同一控制器上的GET和POST操作之间保留JSP文件中未显式引用的值。这是一个省略了错误检查的示例 我有一个域对象 class foo { private int fieldA; private String fieldB; // .. getters and setters omitted } 控制器 @Controller public class MyController { @Autowired private IDaoService daoSe

我的域对象不会在同一控制器上的GET和POST操作之间保留JSP文件中未显式引用的值。这是一个省略了错误检查的示例

我有一个域对象

class foo
{
    private int fieldA;
    private String fieldB;
    // .. getters and setters omitted
}
控制器

@Controller
public class MyController
{
@Autowired
private IDaoService daoService;

@RequestMapping(value = "/display", method = RequestMethod.GET)
public String newForm(@RequestParam("id") Long id, Model model, Principal principal) throws Exception
{
    // check that the user has permissions
    ...

    // get the object
    Foo foo = daoService.read(id);
    // at this point foo.fieldA is equal to the input id
    model.addAttribute("foo", foo);

    // return the JSP path
}

@RequestMapping(value="/update", method = RequestMethod.POST)
public String update(@ModelAttribute("foo") Foo foo,
        BindingResult result, 
        Principal principal,
        Model model) throws Exception

{   
    // ERROR - at this point, fieldA is null

}
}
JSP

    <form:form  method="post" commandName="foo">
        <fieldset>
            <legend>Invest</legend>
            <div class="fm-req">
                <label for="fm-name">Field B</label>    
                <spring:bind path="fieldB">
                    <input id="fm-name" type="text" name="${status.expression}" value="${status.value}" ></input>
                </spring:bind>
            </div>
            <div id="fm-submit" class="fm-req">
                <INPUT type="submit" value="Submit" name="Submit" />        
            </div>
        </fieldset>
    </form:form>                

投资
B区
我本以为JSP会获得以新形式创建的对象,该对象具有fieldA集合(可能还有fieldB)。用户可以选择更改字段B,然后点击提交。 我已经阅读了大量Spring文档并查看了网站,但无法找到为什么控制器中update方法上的foo.fieldA为null

从我对SpringMVC的理解来看,这似乎是一种标准模式,但请随意纠正我


提前感谢,

这是预期的行为

对象
foo
的两个实例(在
newForm
update
中)(在java类名中应以大写字母开头)彼此完全独立

因此,请创建一个隐藏字段,或将其放入会话中。
我建议使用隐藏字段:

您可以使用以下字段之一:

  • 使用拉尔夫的隐藏场方法
  • 将Foo.fieldA的类型更改为私有整数Foo

  • 原因可能是:Foo.fieldA产生了问题,因为NULL值被设置为int type fieldA。

    如果模型不在会话中,为什么会这样?我假设newForm()的模型输入是会话中的模型,因为JSP可以访问命令对象。我是否需要创建新模型,或者如何获取会话中的模型?ThxBy默认模型存在于请求中。是否有任何特殊原因使您使用隐藏字段而不是会话?如果Foo(!)对象有20个字段,那么我不需要定义20个隐藏字段吗?如果字段在html文件中,那么它们会强烈附加到该表单上。->如果用户使用不同的选项卡或多个浏览器,则没有问题。我不需要花时间去思考如何从会议中清除未使用的内容。(另一个原因是隐藏字段并没有给服务器带来太多的负载)——我有一种强烈的感觉,那就是把属于gether的所有想法都放在同一个位置(不是html和session中的spitet)会更干净。愿我们的问题出现在另一个地方。-可能您应该使用不同的类—一个用于域实体,另一个用于更新表单/命令。此表单/命令对象应仅包含表单具有的字段。