[英]How to handle Action's form data that is not coming from JSP in Struts 2?
我在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>
在struts.xml中
<package name="example" namespace="/" extends="default">
<action name="searchAction" class="example.SearchAction">
<result>/example/search.jsp</result>
</action>
</package>
然后是SearchAction類
public class SearchAction extends ActionSupport {
private String id;
private String name;
@Override
public String execute() throws Exception {
if ("".equals(id.trim())) { //#1
...
}
...
}
請參閱#1行代碼,如果我從search.jsp中的“表單”中單擊了“提交”按鈕,則id字段將不為空,但是如果我直接打開http://127.0.0.1:8080/myapps/searchAction.action
,則id字段將為空。
在這種情況下,我可以在SearchAction.exeucte()中使用字段檢查,例如if(id!= null) ,但是我懷疑這是否是最優雅的方法。
有人可以建議更好的方法嗎?
private String id="";
\\快速解決。
但是應用程序不應允許用戶直接點擊操作,否則用戶應先經過身份驗證和/或授權。
在Web環境中,通常很難分辨用戶是否提供了輸入(空vs空)。 您可以使用Struts2驗證http://struts.apache.org/2.x/docs/validation.html驗證值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.