struts中的request.getParameterValues
我想将我以前的 J2EE 招聘应用程序从 servlet 转换为 struts,并且我想知道是否可以将 request.getParameterValues("name") 放在 Action 类中。下面是一个示例代码:
public class ConfirmEditApplicantAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
String forward = "success";
**String screenNames[] = request.getParameterValues("screenName");**
//some codes here....
return mapping.findForward(forward);
}
}
来自 jsp 中的示例表单:
<div id="screenInformation" class="tab_content">
<h4>Screenings:</h4>
<form action="EditScreeningServlet" method="post">
<input type = "hidden" name ="applicantNumber" value="${infoObj.applicantNumber}" >
<table>
<c:forEach var="screen" items="${screenList}">
<input type = "hidden" name ="screenId" value="${screen.screenId}" >
<tr>
<td>Screen Type:  </td> <td><input type="text" value="${screen.screenName}" name="screenName" readonly ="true"></td>
</tr>
<tr>
<td>Date: </td> <td><input type="text" value="${screen.screenDate}" name="screenDate" class="date"></td>
</tr>
<tr>
<td>Result: </td>
<td>
<select name = screenResult>
<option value="Pass" ${screen.screenResult == 'Pass' ? 'selected' : ''}>Pass</option>
<option value="Fail" ${screen.screenResult == 'Fail' ? 'selected' : ''}>Fail</option>
<option value="" ${screen.screenResult == '' ? 'selected' : ''}></option>
</select>
</td>
</tr>
<tr><td> </td><td>  </td></tr>
</c:forEach>
</table>
<input type="submit" class="saveButton" value="SAVE">
</form>
I want to convert my previous J2EE recruitment application from servlet to struts and I wanted to know if it is okay to put a request.getParameterValues("name") inside an Action class. Here is a sample code:
public class ConfirmEditApplicantAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
String forward = "success";
**String screenNames[] = request.getParameterValues("screenName");**
//some codes here....
return mapping.findForward(forward);
}
}
that came from this sample form in jsp:
<div id="screenInformation" class="tab_content">
<h4>Screenings:</h4>
<form action="EditScreeningServlet" method="post">
<input type = "hidden" name ="applicantNumber" value="${infoObj.applicantNumber}" >
<table>
<c:forEach var="screen" items="${screenList}">
<input type = "hidden" name ="screenId" value="${screen.screenId}" >
<tr>
<td>Screen Type:  </td> <td><input type="text" value="${screen.screenName}" name="screenName" readonly ="true"></td>
</tr>
<tr>
<td>Date: </td> <td><input type="text" value="${screen.screenDate}" name="screenDate" class="date"></td>
</tr>
<tr>
<td>Result: </td>
<td>
<select name = screenResult>
<option value="Pass" ${screen.screenResult == 'Pass' ? 'selected' : ''}>Pass</option>
<option value="Fail" ${screen.screenResult == 'Fail' ? 'selected' : ''}>Fail</option>
<option value="" ${screen.screenResult == '' ? 'selected' : ''}></option>
</select>
</td>
</tr>
<tr><td> </td><td>  </td></tr>
</c:forEach>
</table>
<input type="submit" class="saveButton" value="SAVE">
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你可以做到这一点,但要坚持 MVC 设计,可能最好花一些时间来了解 ActionForm.这样,您就可以在扩展 ActionForm 的 java 类中对表单进行验证。在ConfirmEditApplicantAction类中,可以在这里编写业务逻辑,更加系统化。
I think you can do that but to adhere to the MVC design, probably it would be best to spend some time to learn about the ActionForm. That way, you can do validation to your form in a java class that extends ActionForm. In class ConfirmEditApplicantAction, you can code the business logic here which is more systematic.
我不明白为什么它不应该好。您是否实际运行过该代码以查看其是否有效?如果确实如此,那么我认为它没有任何问题,应该没问题。如果没有,那么很可能是不行,因为你没有有效的代码。
I don't see why it shouldn't be OK. Have you actually run that code to see if it works? If it does, then I don't see any problem with it and it should be OK. And if it doesn't, well, then it most likely is not OK since you have no working code.
Struts 处理请求参数的方式是在 ActionForm 中放置一个与参数同名的属性,然后让 Struts 为您填充它。这样,您根本不必处理请求参数,而是处理 ActionForm 属性。
所以,回答你的问题,而不是编码
code:
<%= request.getParameter("name") %>
我会使用 getName() 和 setName() 方法在 ActionForm 中编写属性“name”。
然后在我的 jsp 中编写代码:
code:
或 ..和 <%=用户名%>
如果使用 name 参数调用 jsp,struts 将自动使用 name 属性填充 form bean。
这就是为什么据我所知没有 struts 标签来显示请求参数。
然而jstl中有一个:
The struts way to deal with request parameters is to put a property of the same name as the parameter in the ActionForm, and let Struts populate it for you. This way, you don't have to deal with request parameters at all, but rather, are dealing with ActionForm properties.
So, to answer your question, instead of coding
code:
<%= request.getParameter("name") %>
I would code a the property "name" in my ActionForm with a getName() and a setName() method.
In my jsp, I would then code:
code:
<bean:write name="MyForm" property="name"/>
OR..
<bean parameter id="username" name="user"/> and <%=username%>
If the jsp is called with a name parameter, struts will automatically populate the form bean with the name property.
This is why there's no struts tag that I know of to display a request parameter.
There is one in jstl, however:
<c:out value="${param.name}" />