使用 Spring MVC 的表单提交 portlet

Form submit portlet with Spring MVC

我正在尝试使用 spring MVC 实现提交表单的 Liferay 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 构建了 war,然后我在 Liferay 门户的 apache tomcat 下部署了 war。到目前为止,一切正常,没有问题。 但是当我尝试 运行 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)

看来我的控制器有问题。 有人可以帮我解决这个问题吗?

您正试图在 submitPerson 方法中获取 "person" 模型属性,并且您正试图在 JSP 中使用 "person" 模型属性。但是,您之前没有设置该模型属性。所以首先在 like handleRenderRequest 方法中设置你的 person 模型属性:

ModelAndView modelAndView = new ModelAndView("welcome");
modelAndView.addObject("person", new Person());

看看这个例子,也许对你有帮助。

你必须正确使用ModelAndView。

Neither BindingResult nor plain target object for bean name available as request attribute

JSP 中的表单标记引发异常,因为模型中没有 "person" 属性。您必须在模型中包含 Person class 的实例。有几种方法可以实现它。用 @ModelAttribute 注释注释的方法可能是最方便的。

我建议在控制器中添加以下方法:

@ModelAttribute("person")
public Person getPerson() {
    return new Person();
}

或者你可以修改render方法(我也去掉了签名中不用的参数):

@RenderMapping
public ModelAndView handleRenderRequest() {
    ModelAndView modelAndView = new ModelAndView("welcome");
    modelAndView.addObject("person", new Person());
    modelAndView.addObject("msg", "Hello Spring MVC");
    return modelAndView; 
}

这是因为您在 jsp 中绑定了 "person" 对象,但是您没有从控制器为变量 "person" 传递任何内容。这就是为什么渲染无法在范围内找到该对象并抛出异常的原因。

在您的控制器方法中添加以下代码行,它就会起作用。

mv.addObject("person", new Person()) // Note : you must use same name in jsp page as defined in first argument

您使用的是哪个版本的 Liferay?

如果 > 6.2 GA1

然后在你的liferay-portlet.xml文件中,添加这个属性,重新编译测试。

<requires-namespaced-parameters>false</requires-namespaced-parameters>

Liferay 默认在请求参数中添加命名空间。你需要禁用它。