Portlet view form with Spring MVC

I am trying to create a Liferay portlet of a submit form using spring MVC.

Model:

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

      

Controller:

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());
    }


}

      

View (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>

      

I built a war with maven and then I deployed a war under the tomcat apache of the Liferay portal. Up to this point, everything works fine without problems. But when I tried to launch the portlet, I got an error in the console. The following stacktrace command describes this:

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)

      

It seems that I have something wrong in my controller. Can anyone help me please fix the problem?

+3


source to share


5 answers


Which version of Liferay are you using?

if it is> 6.2 GA1

Then in liferay-portlet.xml add this attribute and recompile and test again.



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

      

Liferay adds namespace to query parameters by default. You need to disable it.

+4


source


Take a look at this example, maybe its helpful four yous.

You have to use ModelAndView correctly.



Neither BindingResult nor normal target for bean name is available as request attribute

+1


source


The JSP form tag throws an exception because there is no "person" attribute in the model. You must include an instance of the class Person

in the model. There are several ways to achieve this goal. The annotated method is @ModelAttribute

probably the most convenient.

I suggest adding the following method to the controller:

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

      

Or you can change the render method (I also removed the unused options from the signature):

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

      

+1


source


You are trying to get the "person" model attribute in a method submitPerson

and you are trying to use the "person" model attribute in yours JSP

. However, this model attribute was not previously set. So first set the model attribute of your face as a method handleRenderRequest

:

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

      

0


source


This is because you are binding the "person" object in your jsp, but you are not missing anything for the "person" variable from the controller. And so the renderer cannot find this object in scope and throws an exception.

Add below line of code to your controller method and it will work.

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

      

0


source







All Articles