Spring Controller + JSP

时间:2013-11-12 09:57:15

标签: java spring jsp spring-mvc

我使用Spring和JSP来构建应用程序。在该应用程序中,我有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)

请帮忙!

5 个答案:

答案 0 :(得分:3)

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

$ {owner.skill}

你需要遵循java bean标准。

即: - 你应该喜欢这个

 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/

答案 1 :(得分:0)

您应该在将#{skills}添加到模型时直接使用owner。或者,如果要从<c:forEach var="skill" items="${skills}"> 对象访问它,则需要提供公共getter。

@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;
}
}

{{1}}

答案 2 :(得分:0)

尝试更改此行

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

在您的所有者实体类中:

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

答案 3 :(得分:0)

您需要使用ModelAndView返回ModelMap

这样做。

public ModelAndView user(ModelMap model, Principal principal) { 


    return new ModelAndView("agents/agents", model);
}

答案 4 :(得分:0)

问题出在您的getter方法

您将其定义为 getSkill() ,其中应该是 getSkills() ,因为它应该跟随字段的名称。< / p>

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

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