我已经迁移到spring portlet MVC 4.0.2来编写portlet。一切都很好,就像Spring Portlet MVC 3,但是当我从JSP
传递表单值时页面到(动作或渲染)方法我无法获得该值。控制器中的ActionRequest.getParameter()
和RenderRequest.getParameter()
方法总是返回null值(同时我在JSP页面上有很多文本框)。
我的控制器类:
package org.basa.maskan.controller;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("VIEW")
public class mdnApptest {
@RenderMapping
public ModelAndView simpleRenderMethod(RenderRequest renderRequest, RenderResponse
renderResponse) {
ModelAndView view = new ModelAndView("page");
return view;
}
@RequestMapping(method = RequestMethod.POST)
@ActionMapping(params = "page=form_set_values")
public void simpleActionMethod(ActionRequest actionRequest, ActionResponse
actionResponse) {
String n = actionRequest.getParameter("firstname");
printParams("ACTION PARAMETERS:", actionRequest.getParameterMap());
System.out.println("Act ********************* this is the inserted name of the
user : " + n);
actionResponse.setRenderParameter("page", "test");
}
@RenderMapping(params = "page=test")
public ModelAndView renderMethod(RenderRequest renderRequest,
RenderResponse renderResponse) {
String n = renderRequest.getParameter("name");
printParams("Render PARAMETERS:" ,renderRequest.getParameterMap());
System.out.println("*************" + n);
return new ModelAndView("page2");
}
private void printParams(String pefix, Map<String, String[]> map) {
for (String sk : map.keySet()) {
String[] val = map.get(sk);
String rv = "";
for (String vv : val) {
rv = rv + vv + " , ";
}
System.out.println(pefix + " -> " + sk + " = " + rv);
}
}
}
我的JSP:
<%@ page import="java.text.Format" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Set" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="./include.jsp"%>
<%
PortletURL url = renderResponse.createActionURL();
url.setParameter("page","form_set_values");
%>
<form action="<%=url.toString()%>" method="post">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname">
<input type="submit" value="Submit"/>
</form>
n对象始终是null
,Spring中是否有任何我没有使用的新概念?
答案 0 :(得分:0)
首先更改您创建操作网址的方式,如下所示:
<portlet:actionURL var="formSetValuesURL">
<portlet:param name="action" value="formSetValuesAction"></portlet:param>
</portlet:actionURL>
然后将表单操作字段替换为上面生成的字段:
<form action="${formSetValuesURL}" method="post">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname">
<input type="submit" value="Submit"/>
</form>
最后一步是控制器映射方法:
@ActionMapping(params = "action=formSetValuesAction")
public void simpleActionMethod(ActionRequest actionRequest,
ActionResponse actionResponse,
@RequestParam("firstname") String firstName) {
System.out.println("Here you have your fist user: "+firstName);
actionResponse.setRenderParameter("page", "test");
}
答案 1 :(得分:0)
我找到了答案,在更新版本的liferay中,您应该将以下标记添加到liferay-portlet.xml文件中:
<requires-namespaced-parameters>false</requires-namespaced-parameters>
现在您可以构建和部署portlet。
欢呼声