Liferay 6.2中的Spring MVC portlet,模型属性在表单发布时为null

时间:2014-02-26 09:06:48

标签: spring spring-mvc liferay portlet liferay-6

我尝试克隆一个Spring MVC Portlet项目,但在发布表单todo后,属性都为空。

回复可在Github上找到。

这是控制器代码

@Controller
@RequestMapping("VIEW")
public class ToDoListController {

    @RenderMapping
    public String view() {
        return "list";
    }

    @ActionMapping
    public void save(@Valid ToDo toDo, BindingResult result, @CookieValue("JSESSIONID") String jsessionid, 
            PortletSession session, ModelMap modelMap) {
        if (!result.hasErrors()) {
            // could use entityManager to persist; put in session for this example
            List<ToDo> toDos = (List<ToDo>) session.getAttribute("toDos");
            if (toDos == null) {
                toDos = new ArrayList<ToDo>();
            }
            toDos.add(toDo);
            session.setAttribute("toDos", toDos);

            modelMap.put("msg", String.format("You added a TODO: %s", toDo.getTitle()));
        }
    }

    @ModelAttribute
    private ToDo loadModel() {
        return new ToDo();
    }
}

以下是视图:

<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="portlet" uri="http://java.sun.com/portlet_2_0" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<c:if test="${msg ne null}">
    <div class="portlet-msg-success"><c:out value="${msg}" /></div>
</c:if>

<portlet:actionURL var="save" />

<form:form modelAttribute="toDo" action="${save}" method="POST">
    <fieldset>
        <legend>Add a TODO</legend>
        <div>
            <form:label path="title" cssStyle="display:block">Title:</form:label>
            <form:input path="title" />
            <form:errors path="title" cssClass="portlet-msg-error" />
        </div>
        <div>
            <form:label path="due" cssStyle="display:block">Due (MM/DD/YYYY):</form:label>
            <form:input path="due" />
            <form:errors path="due" cssClass="portlet-msg-error" />
        </div>
        <div>
            <form:label path="description" cssStyle="display:block">Description:</form:label>
            <form:textarea path="description" />
            <form:errors path="description" cssClass="portlet-msg-error" />
        </div>
        <div>
            <input type="submit" value="Save" />
        </div>
    </fieldset>
</form:form>

这是上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <mvc:annotation-driven validator="validator" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="cache" value="true" />
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>


    <bean id="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

    <bean id="annotationMethodHandlerAdapter"
        class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="webBindingInitializer">
            <bean id="configurableWebBindingInitializer"
                class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
                <property name="validator">
                    <ref bean="validator" />
                </property>
            </bean>
        </property>
    </bean>
</beans>

=============================================== ======================
更新

以下是工作版本:https://github.com/jzinedine/FirstPortlet

1 个答案:

答案 0 :(得分:3)

在Liferay 6.2中使用Spring MVC时,需要在liferay-portlet.xml中设置<requires-namespaced-parameters>false</requires-namespaced-parameters>。在page末尾的Liferay 6.2文档中对此进行了解释。