jsp请求表单页面:
action的execute方法:
struts-config.xml文件:
i18n_login_success.jsp页面:
<%@taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<form action="i18nlogin.do" method="post">
<bean:message key="user.name" /><input type="text" name="username"/><br/>
<bean:message key="user.password"/><input type="password" name="password"/><br/>
<input type="submit" value="<bean:message key="user.submit"/>"/>
</form>
action的execute方法:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
I18nLoginForm ilf = (I18nLoginForm) form;
//ActionMessages相当于保存ActionMessag(信息)的集合
ActionMessages ams = new ActionMessages();
//建立信息(一次只能创建一条),第一个参数为配置文件中的key值,
//第二个参数为动态填充占位符,如果有多个参数的话可以用object数组设定
ActionMessage am1 = new ActionMessage("user.login.success",ilf.getUsername());
ActionMessage am2 = new ActionMessage("user.login.success1",ilf.getPassword());
ams.add("s1", am1);
ams.add("s2",am2);
//把创建好的信息包告诉struts,struts会有相应的方法处理,
//可以在返回页面中用标签显示这些信息
this.addMessages(request, ams);
return mapping.findForward("success");
}
struts-config.xml文件:
<form-bean name="i18form" type="com.cao.form.I18nLoginForm" />
<action path="/i18nlogin" type="com.cao.action.I18NLoginAction" name="i18form">
<forward name="success" path="/i18n_login_success.jsp"></forward>
<forward name="fail" path="/i18n_login_fail.jsp"></forward>
</action>
i18n_login_success.jsp页面:
<%@taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<%@taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<!-- html:messages标签用于输出action中添加的信息(this.saveMessage()方法),
该标签相当于一个循环,id表示每次循环要输出的内容,他默认的是输出error中的信息,
message属性设置为TRUE则输出message中的信息 -->
<html:messages id="msg" message="true">
<bean:write name="msg"/>
</html:messages>