相关疑难解决方法(0)

Spring绑定列表<Object>到Form:复选框

我似乎无法将我的表单绑定到复选框控件.我在这里阅读了很多帖子并尝试了一些技巧但没有运气.也许一双新鲜的眼睛会有所帮助.

我的控制器:

public String editAccount(@RequestParam("id") String id, Model model) {
    model.addAttribute("account", accountService.getAccount(id));
    model.addAttribute("allRoles", roleService.getRoles());
    return EDIT_ACCOUNT;
}
Run Code Online (Sandbox Code Playgroud)

我的jsp:

<form:form action="" modelAttribute="account">
<form:checkboxes items="${allRoles}" path="roles" itemLabel="name" itemValue="id" delimiter="<br/>"/>
</form>
Run Code Online (Sandbox Code Playgroud)

生成的html:

<span><input id="roles1" name="roles" type="checkbox" value="1"/><label for="roles1">User</label></span><span><br/><input id="roles2" name="roles" type="checkbox" value="2"/><label for="roles2">Admin</label></span><span><br/><input id="roles3" name="roles" type="checkbox" value="3"/><label for="roles3">SuperAdmin</label></span<input type="hidden" name="_roles" value="on"/>
Run Code Online (Sandbox Code Playgroud)

我为每个循环使用了一秒(未显示)以确保模型对象包含角色.确实如此,但是没有选中复选框,当我提交角色对象时总是为空.有人可以告诉我我错过了什么吗?

谢谢

编辑

抱歉,我们意识到查看帐户和角色对象可能会有所帮助:

public class Account {

    private String username, firstName, lastName, email;
    private List<Role> roles;

    @NotNull
    @Size(min = 1, max = 50)
    public String getUsername() {
        return username;
    }

    public …
Run Code Online (Sandbox Code Playgroud)

java checkbox binding spring

8
推荐指数
1
解决办法
3万
查看次数

OpenEntityManagerInViewFilter问题

我已经搜索过这个网站,以获得使OpenEntityManagerInViewFilter工作的答案.我有一个标准的User对象,它引用一个具有多对多关系的角色对象作为集合.当我尝试从控制器编辑我的用户时,我得到了可怕的懒惰初始化异常.在大多数情况下,通过简单地将其添加到您的web.xml,这似乎是非常简单的:

<filter>
    <filter-name>oemInViewFilter</filter-name>
    <filter-class>
        org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>oemInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)

现在我尝试过没有成功的事情(这些是来自网络的各种建议)

  1. 将上面的声明移到web.xml的顶部
  2. 围绕我的控制器方法和/或整个类绕过@Transactional
  3. 显然,将获取类型转换为渴望的作品,但在这里打败了我的意图
  4. 玩我在哪里定义了entityManagerFacorty
  5. 已验证OpenEntityManager存在于惰性init异常中,因此将其触发

关于我读过的唯一一件对我有意义的事情就是因为我的持久层设置如何并且过滤器抓错了而我正在加载两个不同的会话.

继承我控制器中的方法,我从数据库中找到一个用户并导致延迟初始化异常,因为它没有从用户对象中检索角色.

@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
   public String edit(@PathVariable final Integer id, final ModelMap modelMap)
   {
      final User user = userDao.find(id); ******This causes the lazy init exception

      if (user != null)
      {
         modelMap.addAttribute("userInstance", user);
         modelMap.addAttribute("validRoles",  new HashSet<Role>(roleDao.findAll()));
         return "/user/edit";
      }
      return "redirect:/user/list";
   }
Run Code Online (Sandbox Code Playgroud)

这是我的相关设置:

web.xml中:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/board-servlet.xml  *****This file references the file …
Run Code Online (Sandbox Code Playgroud)

spring hibernate spring-mvc

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

标签 统计

spring ×2

binding ×1

checkbox ×1

hibernate ×1

java ×1

spring-mvc ×1