相关疑难解决方法(0)

Spring:@ModelAttribute VS @RequestBody

如果我错了,请纠正我.两者都可用于数据绑定.

问题是何时使用@ModelAttribute?

@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) { }
Run Code Online (Sandbox Code Playgroud)

另外,何时使用@RequestBody?

@RequestMapping(value = "/user/savecontact", method = RequestMethod.POST
public String saveContact(@RequestBody Contact contact){ }
Run Code Online (Sandbox Code Playgroud)

根据我的理解,两者都有类似的用途.

谢谢!!

data-binding spring spring-mvc

48
推荐指数
5
解决办法
3万
查看次数

在Spring MVC 3中提交表单 - 解释

我在理解Spring 3 MVC中的表单提交方式时遇到了问题.

我想要做的是创建一个控制器,它将获取用户的名字并显示给他.不知怎的,我已经做到了,但我真的不明白它是如何工作的.所以..

我有一个看起来像这样的表格:

<form:form method="post" modelAttribute="person">
    <form:label path="firstName">First name</form:label>
    <form:input path="firstName" />
    <br />

    <form:label path="lastName">Last name</form:label>
    <form:input path="lastName" />
    <br />

    <input type="submit" value="Submit" />
</form:form>
Run Code Online (Sandbox Code Playgroud)

我也有一个看起来像这样的控制器:

@Controller
public class HomeController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String showHelloPage(Model model) {
        model.addAttribute("person", new Person());
        return "home";
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public String sayHello(Person person, Model model) {
        model.addAttribute("person", person);
        return "home";
    }
}
Run Code Online (Sandbox Code Playgroud)

要向用户显示欢迎消息,请在JSP页面中使用以下代码:

<c:if test="${not empty person.firstName and not empty person.lastName}"> …
Run Code Online (Sandbox Code Playgroud)

java forms spring spring-mvc

31
推荐指数
1
解决办法
5万
查看次数

如何以一种形式传递@RequestParam 和@ModelAttribute

我想将 @RequestParam 和 @ModelAttribute 混合在一种弹簧形式和控制器中。

我在控制器中做的事情如下:

@RequestMapping("/user/edit/{userId}")
public ModelAndView editUser(@PathVariable String userId, 
    @ModelAttribute User user, BindingResult bindingResult,
    @RequestParam Set<String> groups) {

    if(bindingResults.hasErrors() {
        //return back to form and correct errors
    } else {
        //save data and get out of form 
    }
} 
Run Code Online (Sandbox Code Playgroud)

有简单的用户 bean(id、firstName、lastName 等)但没有“groups”属性。还有简单的 Group bean(id、名称、描述),但与用户没有任何联系。所以在逻辑层面上用户和组是完全分离的。

在我的表单中,编辑用户时,有一个包含所有组的 id 的 html SELECT 元素。您可以选择多个 id 并填充用户数据并将其发送到控制器。

到目前为止,一切都运行良好。我正确填写了@ModelAttibute User。我还得到了填充了选定 ID(字符串)的 @RequestParam Set[String] 组。

现在我想做更多的事情。我需要写一些东西,而不是@RequestParam Set[String] 组会给我@RequestParam Set[Group] 组。当然,我可以直接在我的控制器方法 editUser(...) 中转换它,但这不是一个很好的解决方案。所以我决定编写一个自定义的 @InitBinder 方法,它可以很好地为我完成它。

问题来了。

我写了 initBinder 如下:

[...]
webDataBinder.registerCustomEditor(Set.class, "groups", new CustomCollectionEditor(Set.class) { …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc

5
推荐指数
1
解决办法
5218
查看次数

究竟什么是@RequestParam以及它是如何填充的?

Spring文档说:

使用@RequestParam批注将请求参数绑定到控制器中的方法参数.

来源:http: //static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestparam

AFAIK,请求参数是在请求方法为GET时从查询字符串中检索的变量.它们也是请求方法为POST时从表单值检索的变量.我已经使用一个简单的JSP验证了这一点,该JSP通过方法request.getParameter("key")显示请求参数.

但在我看来,@ RequestParam仅适用于GET方法请求.它只能从查询字符串中获取值.

这是文档中的错误吗?有人可以引用一些文档来准确描述@RequestParam用于什么,它不能用于什么,以及它是如何填充的?

我可以使用@RequestParam for POST方法来获取表单值吗?如果我不能使用@RequestParam,我还能用什么?我试图避免调用request.getParameter("key").

java model-view-controller spring spring-mvc

4
推荐指数
1
解决办法
2万
查看次数

春季启动与Thymeleaf帖子列表

我想将一个字符串列表发布到我的控制器.但它始终只取第一个选定的值.

我的thymeleaf html表格:

<form action="/add" method="post">
    <div class="form-group">
        <label for="roleId">ID</label> <input type="text" class="form-control" id="roleId" name="id" required="required" />
    </div>
    <div class="form-group">
        <label for="rolePrivileges">Privileges</label>
        <select class="form-control" id="rolePrivileges" name="privileges" multiple="multiple" size="10" required="required">
            <option th:each="type : ${privilegesList}" th:value="${type}" th:text="${type}">Privilege</option>
        </select>
    </div>
    <button type="submit" class="btn btn-default">Create</button>
</form>
Run Code Online (Sandbox Code Playgroud)

我的控制器:

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addSomething(Model model, @ModelAttribute("id") String id,
        @ModelAttribute("privileges") List<String> privileges) {
    // add something with a service
    return "redirect:/roles";
}
Run Code Online (Sandbox Code Playgroud)

spring-mvc thymeleaf spring-boot

2
推荐指数
1
解决办法
1196
查看次数

如何防止 Spring 4.0 MVC @ModelAttribute 变量出现在 URL 中?

我有一种方法可以让您评论客户,并在添加评论后再次将您重定向到带有评论的网站。

@RequestMapping(value="customers/details/{id}", method = RequestMethod.GET)
    public String showCustomerComments(@ModelAttribute("commentContent") String commentContent, @PathVariable int id, Model model){

        model.addAttribute("comment",commentRepository.getAllComments(id));

        return "details";
    }

    @RequestMapping(value ="customers/details/{id}", method = RequestMethod.POST)
    public String processAddCustomerComment(@ModelAttribute("commentContent") String commentContent, @PathVariable int id){

        commentRepository.
                addComment(commentContent, localDate.now().toString(), id);

        return "redirect:/customers/details/{id}";
    }
}
Run Code Online (Sandbox Code Playgroud)

一切正常,但在 url 中出现模型可验证:

http://localhost:8080/customers/details/62?commentContent=some_text
Run Code Online (Sandbox Code Playgroud)

我已经知道解决方案,但我不知道如何实施。解决方案是在 上设置ignoreDefaultModelOnRedirecttrue ignoreDefaultModelOnRedirect。在本主题中,在此处输入链接描述,他们说只需将 sth like 放入<mvc:annotation-driven ignoreDefaultModelOnRedirect="true" />我们的 xml 文件中。但是如何在基于 Java 的配置中做到这一点?

我有一个这样的课程:

    @Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.packt.webstore")
public class WebConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() { …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc

1
推荐指数
1
解决办法
1719
查看次数