1

我正在使用带有 JSP 的 Spring 来构建应用程序。在该应用程序中,我有 2 个实体:

@Entity
public class Owner implements Serializable {
    // Some fileds

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "owner")
    @Cascade({CascadeType.ALL})
    private List<Skill> skills = new ArrayList<Skill>();

    // Constructor + Getters/Setters

    public List<Skill> getSkill(){
        return this.skills;
   }

    public void setSkillSet(List<Skill> skills){
        this.skills = skills;
   }
}

@Entity
public class Skill implements Serializable {
    // Some fileds

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "OWNER_ID", nullable = false)
    private Owner owner;

    // Constructor + Getters/Setters
}

我的 Spring 控制器代码:

public String user(ModelMap model, Principal principal) {
        try{
            logger.info("CALLING OWNERSERVICE->findAgentsSkills");
            List<String> skills = skillService.findSkillsByAdmin(1);
            logger.info("ENDING OWNERSERVICE->findAgentsSkills");

            logger.info("CALLING OWNERSERVICE->findAgentsSkills");
            List<Owner> owners = ownerService.findAgentsSkills(1);
            logger.info("ENDING OWNERSERVICE->findAgentsSkills");

            model.addAttribute("skills", skills);
            model.addAttribute("owners", owners);
            model.addAttribute("userForm", new UserForm());
        }
        catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return "agents/agents";
    }

最后是我的 JSP:

 <tbody id="agentsTbody" class="filter">
        <c:forEach var="owner" items="${owners}">
            <c:forEach var="skill" items="${owner.skills}">
                 <c:choose>
                     <c:when test="${not empty skill.name}"><td>Oui</td></c:when>
                     <c:otherwise><td>Non</td></c:otherwise>
                 </c:choose>
            </c:forEach>
        </c:forEach>
    </tbody>

我在这一行收到一个错误,<c:forEach var="skill" items="${owner.skills}">告诉我没有skills属性 pn 类型Owner

错误:

org.apache.jasper.el.JspPropertyNotFoundException: /WEB-INF/views/agents/agents.jsp(54,6) '${owner.skills}' Property 'skills' not found on type fr.iconsultants.ibox.model.Owner
    at org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:109)
    at org.apache.jsp.WEB_002dINF.views.agents.agents_jsp._jspx_meth_c_005fforEach_005f2(agents_jsp.java:677)
    at org.apache.jsp.WEB_002dINF.views.agents.agents_jsp._jspx_meth_c_005fforEach_005f1(agents_jsp.java:636)
    at org.apache.jsp.WEB_002dINF.views.agents.agents_jsp._jspService(agents_jsp.java:172)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

请帮忙!

4

5 回答 5

3

你应该使用技能而不是技能

${所有者.技能}

你需要遵循java beans标准。

即:-你应该有这样的

 private List<Skills> skill = new ArrayList<>();


public List<Skills> getSkill() {
    return skill;
}

public void setSkill(List<Skills> skills) {
    this.skill = skill;
}

编辑:- javabean 命名约定

http://blog.codejava.net/nam/naming-convention-for-getter-and-setter/

于 2013-11-12T10:34:00.770 回答
0

问题出在你的getter方法上

您正在像这样定义它getSkill(),它应该是这样getSkills()的,因为它应该遵循字段的名称。

private List<Skill> skills = new ArrayList<Skill>();

public List<Skill> getSkills(){
    return this.skills;
}
于 2013-11-12T10:41:04.413 回答
0

您需要返回 ModelAndView.ModelMap

这样做。

public ModelAndView user(ModelMap model, Principal principal) { 


    return new ModelAndView("agents/agents", model);
}
于 2013-11-12T10:15:58.300 回答
0

您应该在#{skills}将其添加到模型时直接使用。或者如果你想从owner对象中访问它,你需要提供一个公共的 getter。

<c:forEach var="skill" items="${skills}">

或者

@Entity
public class Owner implements Serializable {
// Some fileds

@OneToMany(fetch = FetchType.LAZY, mappedBy = "owner")
@Cascade({CascadeType.ALL})
private List<Skill> skills = new ArrayList<Skill>();

public List<Skill> getSkills(){
    return skills;
}
}
于 2013-11-12T10:01:09.960 回答
0

尝试更改此行

@OneToMany(fetch = FetchType.LAZY, mappedBy = "owner")

在您的所有者实体类中:

@OneToMany(fetch = FetchType.EAGER, mappedBy = "owner")
于 2013-11-12T10:02:06.847 回答