gpt4 book ai didi

java - 使用 Spring MVC 表单提交 portlet

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:43:29 26 4
gpt4 key购买 nike

我正在尝试使用 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;
}
}

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


}

查看(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)

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

最佳答案

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

如果是 > 6.2 GA1

然后在您的liferay-portlet.xml 文件中,请添加此属性并重新编译并再次测试。

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

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

关于java - 使用 Spring MVC 表单提交 portlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30976746/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com