Ara*_*tta 4 java spring-mvc tld
我需要帮助.我正在开展一个项目,我有多个页面和多个表格; 每个页面都有一个表单.我只需要能够将值从一个jsp传递到另一个jsp.我该怎么办?
我是Spring MVC的新手.我使用的是2.5.6弹簧.
这是我的设计:
formPage1.jsp - > Controller1 - > formPage2a.jsp - > Controller2需要val frm pg1&pg2a.formPage1.jsp - > Controller1 - > formPage2b.jsp - > Controller3需要val frm pg1&pg2b.formPage1.jsp - > Controller1 - > formPage2c.jsp - > Controller4需要val frm pg1&pg2c.
如上所示,formPage1.jsp可以加载formPage2a,formPage2b或formPage2c.根据formPage1.jsp中提供的输入,它转到控制器(它是SimpleFormController的扩展),控制器获取user = command对象输入的值.
我希望能够在将formPage2a,formPage2b或formPage2c提交给另一个控制器时使用这些命令对象值.
这是当前的代码:
<form:form method="post" commandName="gainLossRequest">
<form:errors path="*" cssClass="error"/>
<table>
<tr>
<td>
<table>
<tr>
<td><h4>Choose Client</h4></td>
<td style="font-size: medium; font-family: Arial, bold; color: red">*</td>
</tr>
</table>
</td>
<td>
<form:select path="client">
<form:option value="none" label="Select" />
<form:option value="abc" label="abc" />
<form:option value="def" label="def" />
<form:option value="xyz" label="xyz" />
</form:select>
</td>
</tr>
<tr>
<td colspan="2">
<input type="reset" value="Reset" />
<input type="submit" value="Next" />
</td>
</tr>
</table>
</form:form>
Run Code Online (Sandbox Code Playgroud)
public class TestController extends SimpleFormController {
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
public TestController() {
logger.info("entering TestController.constructor..");
setCommandClass(UserPreference.class);
setCommandName("userPreference");
}
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws ServletException {
logger.info("entering TestController.onSubmit all..");
UserPreference userPreference = (UserPreference) command;
ModelAndView view = null;
if ("abc".equals(userPreference.getClient())) {
GainLossRequest gainLossRequest = new GainLossRequest(userPreference);
view = new ModelAndView("redirect:/test/gainLossRequest.htm",
"gainLossRequest", gainLossRequest);
} else if ("def".equals(userPreference.getClient())) {
IncomingPositionsRequest incomingPositionsRequest = new IncomingPositionsRequest();
view = new ModelAndView(
"redirect:/test/incomingPositionsRequest.htm",
"incomingPositionsRequest", incomingPositionsRequest);
} else if ("xyz".equals(userPreference
.getClient())) {
TaxStrategyRequest taxStrategyRequest = new TaxStrategyRequest();
view = new ModelAndView("redirect:/test/taxStrategyRequest.htm",
"taxStrategyRequest", taxStrategyRequest);
}
}
}
Run Code Online (Sandbox Code Playgroud)
<form:form method="post" commandName="gainLossRequest">
<form:errors path="*" cssClass="error"/>
<table style="width: 60%">
<tr>
<td>Account Number (s):</td>
<td style="font-size: medium; font-family: Arial, bold; color: red">*</td>
</tr>
<tr>
<td>
User Chosen Client:
</td>
<td>
<c:out value="${gainLossRequest.client}"/>
</td>
</tr>
<tr colspan="2">
<td>
<input type="reset" value="Reset" />
<input type="submit" value="Submit" />
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
<!-- setupNew.jsp is the first jsp -->
<bean name="/test/setupNew.htm" class="chimeraweb.web.TestController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="userPreference"/>
<property name="commandClass" value="chimeraweb.service.UserPreference"/>
<property name="validator">
<bean class="chimeraweb.service.UserPreferenceValidator"/>
</property>
<property name="formView" value="/test/setupNew"/>
</bean>
<!-- gainLossRequest.jsp is the 2nd jsp where I want to display the values captured in the first jsp page -->
<bean name="/test/gainLossRequest.htm" class="chimeraweb.web.SimpleGainLossController">
<property name="sessionForm" value="true"/>
<property name="commandName" value="gainLossRequest"/>
<property name="commandClass" value="chimeraweb.service.GainLossRequest"/>
<property name="validator">
<bean class="chimeraweb.service.GainLossValidator"/>
</property>
<property name="formView" value="/test/gainLossRequest"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
请帮忙!!
小智 7
您也可以手动使用会话属性,例如:
public ModelAndView handleRequest(HttpServletRequest request){
request.getSession().getAttribute("nameOfAttribute");
}
Run Code Online (Sandbox Code Playgroud)
对于将其作为带注释的控制器编写道歉,我不记得xml配置控制器是否提供此功能...
没有涉及Spring代码.另一种选择是在控制器上使用@SessionAttribute注释:
@Controller
@SessionAttributes("nameOfAttribute")
public class MyController{
//your session attribute can be accessed in controller methods using @ModelAttribute
public ModelAndView handleRequest(@ModelAttribute("nameOfAttribute")){
session.getAttribute("nameOfAttribute");
}
Run Code Online (Sandbox Code Playgroud)
注意
完成后,您需要清除会话属性:
request.getSession().setAttribute("nameOfAttribute", null);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21787 次 |
最近记录: |