我有一个基本的Liferay portlet并使用Spring MVC。我只有一个控制器用于portlet的VIEW模式,只有一个表单。当我提交表格时,不处理行动阶段。
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, "pete@pete.com");
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>
答案 0 :(得分:1)
&#34;行动阶段未得到处理&#34;意思? 它直接渲染阶段?或者你的日志中有一种异常吗?
其他人不可能理解原因...... 起初看起来很好看......顺便说一句,你是否试图以这种方式修改你的actionUrl?
<portlet:actionURL var="submitForm">
<portlet:param name="action" value="validateRequest"></portlet:param>
</portlet:actionURL>
之前,您是否在JSP中导入了portlet taglib?
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>