【问题标题】:Java Spring Boot: Retrieving Model AttributeJava Spring Boot:检索模型属性
【发布时间】:2020-10-23 12:40:58
【问题描述】:

我正在开发一个 Java Spring Boot Web 应用程序,但在检索我想在我的一个页面上显示的模型属性时遇到了问题。该页面是“/profile”,这里是 profile.jsp 页面:

<c:url var="editAccount" value="/editAccount" />
   
<div id="profileAbout">

    <div class="row">
    
        <form:form modelAttribute="user">
    
            <div class="col-md-10 col-md-offset-1">
                    
                <div style="text-align: center; font-weight: bold; font-size: x-large; margin: 1%;">Username: <c:out value="${user.email}" /></div>
    
            </div>
                
            <div class="profile-about-edit">
                <a href="${editAccount}">Edit</a>
            </div>
        
        </form:form>
        
    </div>
    
</div>

下面是控制器方法:

    @RequestMapping(value="/profile")
    public ModelAndView profile(ModelAndView modelAndView, @ModelAttribute("user") SiteUser user) {
        
        SiteUser userVal = getUser();
        Profile profile =  profileService.getUserProfile(userVal);
        
        if(profile == null) {
            profile = new Profile();
            profile.setUser(userVal);
            profileService.save(profile);
        }
        
        Profile webProfile = new Profile();
        webProfile.safeCopyFrom(profile);
        
        modelAndView.getModel().put("profile", webProfile);
        
        modelAndView.setViewName("app.profile");
        
        return modelAndView;
    }

所以,正如您在 profile.jsp 中看到的,我正在尝试显示 user.email,但由于某种原因无法显示。我有这个适用于其他模型属性,但我不确定如何处理这个。我还附上了一张图片来说明我的问题:

这是 Profile 的实体:

@Entity
@Table(name="Profile")
public class Profile {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="profileId")
    private long profileId;
    
    @OneToOne(targetEntity=SiteUser.class)
    @JoinColumn(name="user_id", nullable=false)
    private SiteUser user;
    
    @Column(name="about", length=1000)
    @Size(max=1000, message="{edit.profile.about.size}")
    private String about;
    
   ...

}
    

这里是 SiteUser 的实体:

@Entity
@Table(name="Users")
@PasswordMatch(message="{register.repeatPassword.mismatch}")
public class SiteUser {
    
    @Id
    @Column(name="userId")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long userId;

    @Column(name="email", unique=true)
    @Email(message="{register.email.invalid}")
    @NotBlank(message="{register.email.invalid}")
    private String email;

    ...
}

【问题讨论】:

  • 将此更改为

标签: java spring spring-boot modelattribute


【解决方案1】:

如果您想要显示数据,为什么还需要表单?我假设所有的编辑都将在页面 /edit-profile 上完成。

此外,此页面的模型属性似乎是 profile,而不是 user

考虑到以上所有因素,我认为您的页面应该如下所示:

<c:url var="editAccount" value="/editAccount" />
<div id="profileAbout">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div style="text-align: center; font-weight: bold; font-size: x-large; margin: 1%;">Username: <c:out value="${profile.user.email}" /></div>
            </div>
            <div class="profile-about-edit">
                <a href="${editAccount}">Edit</a>
            </div>
            </div>
     </div>
</div>

【讨论】:

    【解决方案2】:

    试试这个。您应该使用配置文件访问它。

    ${profile.email} 
    

    【讨论】:

    • 问题是邮件不是配置文件的属性,只是用户的属性,所以这不起作用。
    • 我已经在上面的问题中发布了配置文件和用户控制器。
    【解决方案3】:

    好吧,看起来 Profile 和 SiteUser 在语义上非常紧密,你真的应该考虑只使用 1 个包含 Profile 和 SiteUser 信息的 bean。

    话虽如此,您可以使用两个类的信息创建一个新类,比如 ProfilePageModel,并在您的视图中使用它。

    【讨论】:

      【解决方案4】:
          @RequestMapping(value = "/profile")
          public ModelAndView profile(final ModelAndView modelAndView, @ModelAttribute("user") final SiteUser user) {
      

      至少当上述方法处理GET请求时,user只是实例化,emailnull

      您需要填写user 的属性才能显示${user.email}

      另见:

      【讨论】:

        【解决方案5】:

        你可以尝试添加modelAndView.getModel().put("user", webProfile.getUser());

        或者你可以尝试添加&lt;form:form modelAttribute="profile"&gt;,当你显示它时使用${profile.user.email}

        【讨论】:

          猜你喜欢
          • 2016-07-12
          • 2018-12-15
          • 1970-01-01
          • 2022-01-25
          • 2014-10-17
          • 2021-10-08
          • 1970-01-01
          • 2018-12-16
          相关资源
          最近更新 更多