我正在嘗試使用spring MVC實現提交表單的Liferay portlet。表單使用Spring MVC提交portlet
模型:
package com.model;
public class Person {
String firstName;
String middleName;
public String getFirstName()
{
return this.firstName;
}
public String getMiddleName()
{
return this.middleName;
}
public void setFirstName(String firstName)
{
this.firstName=firstName;
}
public void setMiddleName(String middleName)
{
this.middleName=middleName;
}
}
控制器:
package com.controller;
import com.model.Person;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
@Controller(value = "MyFirstSpringMVCPortlet")
@RequestMapping("VIEW")
public class MyFirstSpringMVCPortlet {
@RenderMapping
public ModelAndView handleRenderRequest() {
ModelAndView modelAndView = new ModelAndView("welcome");
modelAndView.addObject("person", new Person());
modelAndView.addObject("msg", "Hello Spring MVC");
return modelAndView;
}
@ActionMapping(value = "handleSubmitPerson")
public void submitPerson(
@ModelAttribute("person") Person person,
ActionRequest actionRequest, ActionResponse actionResponse,
Model model) {
System.out.println("FirstName= "+person.getFirstName());
System.out.println("MiddleName= "+person.getMiddleName());
}
}
視圖(的welcome.jsp)
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<h1>${msg}</h1>
<portlet:defineObjects />
<portlet:actionURL var="submitFormURL" name="handleSubmitPerson"/>
<form:form name="person" method="post" modelAttribute="person" action="<%=submitFormURL.toString() %>">
<br/>
<table style="margin-left:80px">
<tbody>
<tr>
<td><form:label path="firstName">First Name</form:label></td>
<td><form:input path="firstName"></form:input></td>
</tr>
<tr>
<td><form:label path="middleName">Middle Name</form:label></td>
<td><form:input path="middleName"></form:input></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit Form">
</td>
</tr>
</tbody>
</table>
</form:form>
我用maven構建了這場戰爭,然後我在Liferay門戶的Apache Tomcat下部署了這場戰爭。直到這一點,一切工作正常,沒有問題。 但是,當我試圖運行portlet時,我在控制檯中出現了一個錯誤。下面的堆棧跟蹤描述它:
11:37:15,586 ERROR [RuntimePageImpl-26][render_portlet_jsp:132] null
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'person' available as request attribute
at org.apache.jsp.WEB_002dINF.jsp.welcome_jsp._jspx_meth_form_005flabel_005f0(welcome_jsp.java:238)
at org.apache.jsp.WEB_002dINF.jsp.welcome_jsp._jspService(welcome_jsp.java:173)
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 com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:96)
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.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:605)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:544)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
看來我的控制器有問題。 有人可以幫我解決問題嗎?
謝謝Sharana它工作正常! –