[英]How to handle Action's form data that is not coming from JSP in Struts 2?
I have a form in search.jsp 我在search.jsp中有一个表单
<s:form name="employeeSearchForm" action="searchAction" method="post">
<s:textfield name="id" label="Employee ID"/>
<s:textfield name="name" label="Employee Name"/>
<s:submit/>
</s:form>
In struts.xml 在struts.xml中
<package name="example" namespace="/" extends="default">
<action name="searchAction" class="example.SearchAction">
<result>/example/search.jsp</result>
</action>
</package>
Then the SearchAction class 然后是SearchAction类
public class SearchAction extends ActionSupport {
private String id;
private String name;
@Override
public String execute() throws Exception {
if ("".equals(id.trim())) { //#1
...
}
...
}
See the #1 line code, if I clicked submit button from the Form in search.jsp, the id field will not be null, but if I open http://127.0.0.1:8080/myapps/searchAction.action
directly, id field will be null. 请参阅#1行代码,如果我从search.jsp中的“表单”中单击了“提交”按钮,则id字段将不为空,但是如果我直接打开http://127.0.0.1:8080/myapps/searchAction.action
,则id字段将为空。
In this case, I can use field check in SearchAction.exeucte(), eg if (id != null) , but I doubt if it's the most elegant way. 在这种情况下,我可以在SearchAction.exeucte()中使用字段检查,例如if(id!= null) ,但是我怀疑这是否是最优雅的方法。
Anyone can suggest better way? 有人可以建议更好的方法吗?
private String id="";
\\quick fix. \\快速解决。
But app shouldn't allow the user hitting the action directly user should be authenticated and/or authorized before. 但是应用程序不应允许用户直接点击操作,否则用户应先经过身份验证和/或授权。
In web environment it is usually hard to tell whether user provided input or not (empty vs null). 在Web环境中,通常很难分辨用户是否提供了输入(空vs空)。 In your case you can validate values using Struts2 validation http://struts.apache.org/2.x/docs/validation.html . 您可以使用Struts2验证http://struts.apache.org/2.x/docs/validation.html验证值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.