I have a basic Liferay portlet and use Spring MVC. I have only one controller for VIEW mode of portlet and only one form. When I submit the form, action phase is not handled.
GraphViewController.java
package graphui;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
/**
*
* Controller for VIEW mode of portlet.
*/
@Controller("graphViewController")
@RequestMapping(value = "VIEW")
public class GraphViewController{
private static final String MODEL_KEY_NAME = "name";
private static final String MODEL_KEY_AGE = "age";
private static final String MODEL_KEY_EMAIL = "email";
private Person repository = new Person("pete", 33, "[email protected]");
Gson gson = new GsonBuilder().setPrettyPrinting().create();
private static final Logger logger = Logger.getLogger(GraphViewController.class);
@ActionMapping(GraphUIConstants.SUBMIT_FORM)
public void handleActionRequest(ActionRequest request, ActionResponse response)throws Exception {
logger.info("action phase executed");
if (request.getParameter("name") != null)
repository.setName(request.getParameter("name"));
if (request.getParameter("age") != null)
repository.setAge(Integer.parseInt(request.getParameter("age")));
if (request.getParameter("email") != null)
repository.setEmail(request.getParameter("email"));
}
@RenderMapping
public ModelAndView handleRenderRequest(RenderRequest request, RenderResponse response, ModelMap model) {
logger.info("render phase executed");
model.addAttribute(MODEL_KEY_NAME, gson.toJson(repository.getName()));
model.addAttribute(MODEL_KEY_AGE, gson.toJson(repository.getAge()));
model.addAttribute(MODEL_KEY_EMAIL, gson.toJson(repository.getEmail()));
return new ModelAndView("index", model);
}
}
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="graphui.GraphUIConstants" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<portlet:actionURL name="<%= GraphUIConstants.SUBMIT_FORM %>" var="submitForm" />
<div>
<form id="myForm" action="${submitForm}" method="post">
<div class="control-group">
<label class="control-label" for="name">Name:</label>
<div class="controls">
<input name="name" id="name" class="field-required" type="text">
</div>
</div>
<div class="control-group">
<label class="control-label" for="age">Age:</label>
<div class="controls">
<input name="age" id="age" class="field-required field-digits" type="text">
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">E-mail:</label>
<div class="controls">
<input name="email" id="email" class="field-required field-email" type="text">
</div>
</div>
<input class="btn btn-info" type="submit" value="Submit">
<input class="btn btn-primary" type="reset" value="Reset">
</form>
<ul>
<li>${name};</li>
<li>${age};</li>
<li>${email};</li>
</ul>
</div>
Person.java
package graphui;
public class Person {
private String name;
private int age;
private String email;
public Person(String name, int age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
GraphUIConstants.java
package graphui;
public class GraphUIConstants {
public static final String SUBMIT_FORM = "submitForm";
}
GraphUI-portlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="graphui" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
</bean>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.GsonHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<!-- Spring MVC Message Source -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="useCodeAsDefaultMessage" value="true"/>
<property name="basenames">
<list>
<value>content.graph</value>
</list>
</property>
</bean>
</beans>