如何使用Jsp JSTL显示Spring模型?

时间:2013-04-03 09:38:46

标签: java spring jsp jstl

实际上在我的spring应用程序中有User.jsp并使用这个Jsp我显示详细信息。

在我的UserProfileForm bean中,我正在编写

private List<ProfessionalForm> professionalDetails = new ArrayList<ProfessionalForm>();

ProfessionalForm还有另一种形式的细节。

在我的控制器课程中,我正在写作..

 @RequestMapping(value = "user", method = RequestMethod.GET)
    public String user(Model model) throws Exception {
        UserProfileForm form = (UserProfileForm)FormType.USER_PROFILE.getNewInstance();

        model.addAttribute("USER_PROFILE", form);
        return "profile/user";
    }

我的user.jsp页面代码..

<form:form action="" modelAttribute="USER_PROFILE">
   <div class="answer">
    <c:forEach items="${USER_PROFILE.professionalDetails}" var="professionalForm">
        <jsp:include page="user/professional.jsp">
           <jsp:param value="${professionalForm}" name="PROFESSIONAL" />
        </jsp:include>
    </c:forEach>
   </div>
</form:form>

这是我的professional.jsp ..

<form:form action="profile/proffessional" modelAttribute="PROFESSIONAL">
<p>
  <label class="control_label">Postion:</label>
    <div class="controls">
        <form:input class="text_midem" path="positionName" />
    </div>
</p>
</form>

但是我的服务器控制台出现了异常错误。

    SEVERE: Servlet.service() for servlet [spring] in context with path [/EClass] threw exception [An exception occurred processing JSP page /WEB-INF/pages/profile/user.jsp at line 40

37:                         <div class="answer">
38:                             <c:forEach items="${USER_PROFILE.professionalDetails}"
39:                                 var="professionalForm">
40:                                 <jsp:include page="user/professional.jsp">
41:                                     <jsp:param value="${professionalForm}" name="PROFESSIONAL" />
42:                                 </jsp:include>
43:                             </c:forEach>


Stacktrace:] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'PROFESSIONAL' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)

为什么这条消息会出现在User.jsp页面或Professional.jsp中所需的任何循环..

请帮帮我..

编辑:

而不是<jsp:include>我们正在使用...

<%@include file="user/professional.jsp"%>

听到错误即将来临......

    SEVERE: Servlet.service() for servlet [spring] in context with path [/EClass] threw exception [An exception occurred processing JSP page /WEB-INF/pages/profile/user/professional.jsp at line 16

13:                                 <p>
14:                                     <label class="control_label">Postion:</label>
15:                                 <div class="controls">
16:                                   <form:input class="text_midem" path="positionName" />
17:                                 </div>
18:                                 </p>
19:                                 <p>


Stacktrace:] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'PROFESSIONAL' available as request attribute

2 个答案:

答案 0 :(得分:1)

使用&lt;%@ include%&gt;而不是jsp:include

<%@include file="user/professional.jsp"%>

答案 1 :(得分:0)

......
<jsp:include page="user/professional.jsp">
           <jsp:param value="${professionalForm}" name="PROFESSIONAL" />
        </jsp:include>
.......

上述代码将名为PROFESSIONAL的请求参数发送到professional.jsp页面,请求属性。以下代码查找请求属性

....
<form:form action="profile/proffessional" modelAttribute="PROFESSIONAL">
....

所以你需要将值设置为请求属性。

尝试以下代码

.......

<form:form action="" modelAttribute="USER_PROFILE">
   <div class="answer">
    <c:forEach items="${USER_PROFILE.professionalDetails}" var="professionalForm">
        <c:set var="PROFESSIONAL" scope="request" value="${professionalForm}">
        <jsp:include page="user/professional.jsp">           
        </jsp:include>
    </c:forEach>
   </div>
</form:form>
........