1、一個(gè)FormBean繼承ActionForm
成員變量名需要和提交的表單的各屬性名一樣。
2、在struts-config.xml配置文件中配置formbean,例如:
<form-beans>
<form-bean name="userForm" type="cn.iego.wudi.strutsdemo.user.web.struts.forms.UserForm"></form-bean>
</form-beans>
3、配置填充到哪個(gè)Action,需要在struts-config.xml中配置把action的name屬性寫(xiě)成填充的那個(gè)formbean的name,例如:
<action input="/WEB-INF/user/reguser.jsp" name="userForm" path="/RegUser" type="cn.iego.wudi.strutsdemo.user.web.struts.actions.RegUser">
<forward name="success" path="/WEB-INF/user/main.jsp"></forward>
<forward name="failure" path="/WEB-INF/user/reguser.jsp"></forward>
</action>
Action標簽的input屬性,是校驗沒(méi)通過(guò)返回的頁(yè)面。name是formbean的名字。path是action的請求名。type是Action類(lèi)的路徑。
4、Jsp中使用struts中的標簽,需要引用標簽uri
<%@taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<%@taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
5、struts默認關(guān)閉EL表達式,需要在jsp頁(yè)面使用下面的語(yǔ)句打開(kāi)支持
6、配置國際化資源文件需要在src目錄下添加propertis文件
例如我們配置一個(gè)叫Application.propertis文件(默認沒(méi)有后綴),中文地區需要命名為Application_zh_CN.propertis,英文地區命名為Application_en.propertis。
然后我們需要在struts-config.xml配置文件中配置資源文件路徑(根據struts-config的DTD限制需要在<action-mappings>后)
<%@ page isELIgnored="false"%>
7、ActionServlet填充formbean的時(shí)候,formbean通過(guò)
方法進(jìn)行校驗,需要返回一個(gè)ActionErrors 類(lèi)型的,父類(lèi)是ActionMessages,是MAP實(shí)現。使用 add(String property, ActionMessage message) 方法添加錯誤信息,前一個(gè)參數需求填入一個(gè)屬性名,為了以后根據屬性名顯示出錯誤內容,第二個(gè)參數是一個(gè)ActionMessage對象,構造ActionMessage對象,使用ActionMessage(String key, boolean resource) 構造方法,第一參數是國際化資源的key,第二個(gè)參數是判斷是否使用國際化資源,如果第二個(gè)參數為false則不使用,到時(shí)候顯示的錯誤信息就直接是key值。
例如:
<message-resources parameter="cn.iego.wudi.resouces.Application"></message-resources>
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ("".equals(user.getUsername().trim())) {
errors.add("username", new ActionMessage("error.username", true));
}
if (!user.getPass().equals(pass2)) {
errors.add("pass2", new ActionMessage("error.pass2", true));
}
return errors;
}
8、在jsp頁(yè)面中顯示formbean返回的錯誤信息
由于在action中配置了input屬性,則會(huì )返回到input頁(yè)面,并且把錯誤信息也同時(shí)提交給input頁(yè)面,在新的jsp頁(yè)面中可以使用struts標簽中的<html:errors />標簽來(lái)顯示出所有錯誤信息,并且會(huì )默認使用資源文件中的errors.footer、errors.header、errors.prefix、errors.suffix。如果只想顯示一個(gè)錯誤類(lèi)型的信息需要配置property屬性。例如現在不需要默認的header、footer、prefix、suffix并且只想顯示出username的錯誤信息,可以通過(guò)以下代碼實(shí)現。
9、使用struts標簽中的<html:form>
<html:form action="/RegUser" focus="pass2">
action="/RegUser" 屬性為struts-config中action的path,直接生成action請求的地址。focus="pass2"獲取焦點(diǎn),為把光標顯示在哪個(gè)上面。
10、使用國際化資源修改submit控件
Struts中提供了submit控件的標簽
<html:submit titleKey="info.submit"></html:submit>
titleKey屬性是填入一個(gè)資源的key,當鼠標移動(dòng)到這個(gè)submit控件上會(huì )顯示出資源key對應的信息。
<bean:message key="info.submit" />
可以顯示出資源文件中對于key的內容,在這里當作submit的value。
11、action的錯誤消息驗證,實(shí)現資源消息的參數化和參數的國際化
在execute方法中創(chuàng )建一個(gè)
ActionMessages errors = new ActionMessages()對象。
通過(guò)判斷加入錯誤消息,使用add(String property, ActionMessage message) 方法添加錯誤信息,前一個(gè)參數需求填入一個(gè)屬性名,為了以后根據屬性名顯示出錯誤內容,第二個(gè)參數是一個(gè)ActionMessage對象。
例如:
<html:errors header="" footer="" prefix="" suffix="" property="username" />
<html:submit titleKey="info.submit">
<bean:message key="info.submit" />
</html:submit>
UserForm userForm = (UserForm) form;
ActionMessages errors = new ActionMessages();
if (userForm.getUser().getUsername().trim().equals("wd")) {
errors.add("username", new ActionMessage("error.repeated",
getResources(request).getMessage(getLocale(request),
"prompt.username")));
saveErrors(request, errors);
return mapping.getInputForward();
}
資源文件內配置為:
先通過(guò)Action中的getResources(request)得到MessageResources對象,使用MessageResources對象的getMessage(Locale locale, String key) 方法得到當前語(yǔ)言環(huán)境下資源文件的key對應的資源。通過(guò)Action中的getLocale(request)方法獲得當前語(yǔ)言環(huán)境。