我有一个portlet,它能够显示表单,然后允许传递给用户设置的对象中的控制器。 它运作良好。我希望应用程序能够从jsp设置一个额外的属性,但不是来自表单的输入。
模型
public class Person {
String firstName;
String middleName;
String attributeSetStatically;
// Setters and getters
public void setAttributeSetStatically(String attributeFromJsp)
{
System.out.println("Call setAttributeSetStatically "+attributeFromJsp);
this.attributeSetStatically=attributeFromJsp;
}
}
控制器
@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());
System.out.println("attributeSetStatically= "+person.getAttributeSetStatically());
}
}
查看(的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() %>">
${person.setAttributeSetStatically('attributeSetStatically of person')}
<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>
我在控制台中得到的输出是:
FirstName= Kallel
MiddleName= Omar
attributeSetStatically= null
Call setAttributeSetStatically attributeSetStatically of person
因此,在提交后调用$ {person.setAttributeSetStatically('personSetStatically of person')}。为什么?有解决方案可以做我想要的吗?
答案 0 :(得分:1)
可以在表单内的隐藏输入中设置attributeSetStatically
:
<form:input type="hidden" name="attributeSetStatically" value="attributeSetStatically of person">