转入受保护页面的action
if (request.getSession().getAttribute("user") == null) {
ActionForward af = mapping.findForward("login");
18、定义全局的struts-config转发
//先找到局部才找全局的
<global-forwards>
<forward name="login" path="/login.jsp" redirect="true"/>
</global-forwards>
注意配置文件的内容是不允许改的
如ActionForward af = mapping.findForward("login");
//不能改成af.setRedirect(true);
不用struts做转向,自己转向的方法
//重定向
//response.sendRedirect(request.getContextPath() + "/login.jsp");
//return null;
15、动态actionFrom
自定义转向类用classicName
<forward name="success" path="/mustlogin.jsp" className=""/>
动态的做法
Jsp页面
<li>动态ActionForward测试</li><br>
<form action="dynaactionforward.do" method="post">
页面:<input type="text" name="page"><br>
<input type="submit" value="提交">
</form>
配置struts文件
<action path="/dynaactionforward"
type="com.bjsxt.struts.DynaActionForwardTestAction"
>
</action>
配置action
ActionForward af = new ActionForward();
af.setPath("/page" + page + ".jsp?name=Tom");
return af;
19、struts-config.xml文件中,每个<action>标签对应一个ActionMapping实例
注意:Villidate的验证异常抛到input指定的页面上去
<action path="/login"
type="com.bjsxt.struts.LoginAction"
name="loginForm"
scope="request"
validate="false"
input=""
>
注意;转向必须通过配置文件
可以直接配置forward就可以直接转向
Jsp页面
<a href="login1.do">登录(采用jstl)</a><br>
配置struts
<action path="/login1"
forward="/login.jsp"
></action>
配置文件找不到action就配置unknown属性就可以了就是默认页面或容错处理
如<action path="/testunknown"
unknown="true"
forward="/testunknown.jsp"
></action>
注意可以用el表达式子记录已经登陆失败的用户名
也可以用struts标签HTML
页面中会自动保持住actionForm中的数据,一般做国际化才用
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<h1>用户登录(采用struts标签)</h1>
<hr>
<html:form action="login2.do" method="post">
用户:<html:text property="username"/><br>
密码:<html:password property="password"/><br>
<html:submit value="登录"/>
</html:form>
21、分步收集session的使用
在actionFrom中设置
private String name;
private int[] productId;
private String address;
public void resetField() {
this.name = null;
this.productId = null;
this.address = null;
}
在第一个action中设置
StepActionForm saf = (StepActionForm)form;
saf.resetField();
return mapping.findForward("success");
在struts配置文件中
<action-mappings>
<action path="/start"
type="com.bjsxt.struts.StartAction"
name="stepForm"
scope="session"
>
<forward name="success" path="/step1.jsp"/>
</action>
收集确认的jsp页面中
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<h1>确认订单</h1>
<hr>
<form action="finish.do" method="post">
姓名:${stepForm.name }<br>
产品:
<c:forEach items="${stepForm.productId}" var="p" varStatus="vs">
产品${p }
<c:if test="${vs.count != fn:length(stepForm.productId)}" //注意后面)}之间不要有空格 var="v">
,
</c:if>
</c:forEach>
<br>
地址:${stepForm.address }<br>
<input type="submit" value="确认">
</form>
产品收集页面中
<form action="step2.do" method="post">
<input type="checkbox" name="productId" value="1">产品1<br>
<input type="checkbox" name="productId" value="2">产品2<br>
<input type="checkbox" name="productId" value="3">产品3<br>
<input type="checkbox" name="productId" value="4">产品4<br>
<input type="checkbox" name="productId" value="5">产品5<br>
<input type="submit" value="下一步">
</form>
22、国际化i18n
native2ascii命令的位置和用法
一种就是双击打开运行命令,复制进去回车就可以了
第二种就是文件对考
使用native2ascii.exe o.properties MessagesBundle_zh_CN.properties
Java国际化
提供文件以后
代码如下
Locale defaultLocale = Locale.getDefault();
//默认是由操作系统的语言区决定的
System.out.println("default country=" + defaultLocale.getCountry());
System.out.println("default language=" + defaultLocale.getLanguage());
//Locale currentLocale = new Locale("en", "US");
//Locale currentLocale = new Locale("zh", "CN");
Locale currentLocale = new Locale("ja", "JP");
//当找不到时,用系统缺省的文件,如果
//找不到就出错建议有一个缺省的文件
ResourceBundle rb = ResourceBundle.getBundle("res.MessagesBundle", currentLocale);//记得加入目录名
//System.out.println(rb.getString("k1"));
//System.out.println(rb.getString("k2"));
MessageFormat mf = new MessageFormat(rb.getString("k1"));
System.out.println(mf.format(new Object[]{"Tom"}));
//System.out.println(mf.format(new Object[]{"张三"}));//编码成unicorn
}
硬编码国际化
1、struts国际化的配置
* 在struts-config.xml文件中加入:<message-resources parameter="src.MessageResources" />//basename
2、提供不同版本的国际化资源文件,中文需要采用native2ascii转换成unicode
jsp中采用<bean:message>标签来读取国际化消息文本
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<title><bean:message key="user.title"/></title>
自动切换国际化
Jsp页面中
<a href="changelang.do?lang=zh">中文</a>   <a href="changelang.do?lang=en">英文</a>
Action中
String lang = request.getParameter("lang");
Locale currentLocale = Locale.getDefault();
if ("zh".equals(lang)) {
currentLocale = new Locale("zh", "CN");
}else if("en".equals(lang)) {
currentLocale = new Locale("en", "US");
}
//request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);
//等同于
this.setLocale(request, currentLocale);
return mapping.findForward("index");
动态国际化:
1、编程式异常
* 截获异常
* 创建相应的异常消息
* 传递异常消息
* 转向相应的页面处理异常
包括提示信息和错误信息
资源文件
user.login.success={0},Login Success
user.not.found=User Not Found,UserName[{0}]
user.password.error=Password Error
user.login.error=Login Error
action中
LoginActionForm laf = (LoginActionForm)form;
String username = laf.getUsername();
String password = laf.getPassword();
//创建集合
ActionMessages messages = new ActionMessages();
try {
UserManager.getInstance().login(username, password);
//一般性消息
//创建国际化消息文本
ActionMessage message = new ActionMessage("user.login.success", username);
//ActionMessage message = new ActionMessage("user.login.success", new Object[]{username});
messages.add("loginSuccess1", message);
ActionMessage message1 = new ActionMessage("user.login.success", username);
messages.add("loginSuccess2", message1);
//传递国际化消息文本
this.saveMessages(request, messages);
return mapping.findForward("success");
}catch(UserNotFoundException unfe) {
unfe.printStackTrace();
//错误消息
//创建国际化消息文本
ActionMessage message = new ActionMessage("user.not.found", username);
messages.add("error1", message);
//传递国际化消息文本
this.saveErrors(request, messages);
}catch(PasswordErrorException pee) {
pee.printStackTrace();
//创建国际化消息文本
ActionMessage message = new ActionMessage("user.password.error");
messages.add("error2", message);
//传递国际化消息文本
this.saveErrors(request, messages);
}
return mapping.findForward("error");
}
Jsp页面使用
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
成功页面
<!-- id是 循环读取的标签的变量
messages默认是读取错误消息的,要设置message=TRUE
加上property后就是指定了
-->
<html:messages id="msg" message="true" property="loginSuccess1">
<bean:write name="msg"/>
</html:messages>
错误页面
<font color="red">
<li>
<html:messages id="msg" property="error1">
<bean:write name="msg"/>
</html:messages>
</li>
</font>
<font color="blue">
<li>
<html:messages id="msg" property="error2">
<bean:write name="msg"/>
</html:messages>
</li>
</font>
使用只有显示错误的标签,查看MessageResources.properties文件
<html:errors/>
<form action="login.do" method="post">
<bean:message key="user.username"/>:<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.button.login"/>">
</form>
修饰在资源文件中
# -- standard errors --
errors.header=<UL>
errors.prefix=<LI><font color="red">
errors.suffix=</font></LI>
errors.footer=</UL>
通过<html:messages>标签显示消息(可以显示普通消息和错误消息)
通过<html:errors>显示消息(只能显示错误消息)
Jstl国际化参考jstl.pdf
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<fmt:setLocale value="${header['accept-language']}"/>
<fmt:setBundle basename="res.MessageResources"/>
<title><fmt:message key="user.title"/></title>
</head>
<body>
<h1><fmt:message key="user.title"/></h1>
<hr>
<form action="login.do" method="post">
<fmt:message key="user.username"/>:<input type="text" name="username"><br>
<fmt:message key="user.password"/>:<input type="password" name="password"><br>
<input type="submit" value="<fmt:message key="user.button.login"/>">
</form>
</body>
23、动态验证框架
* 加入国际化配置在struts-config.xml文件中,如:
<message-resources parameter="MessageResources" />
* 提供国际化资源文件
* 引入validator插件在struts-config.xml文件中,如:
<!-- 动态验证框架必须继承的类要注意 -->
<form-bean name="loginForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="username" type="java.lang.String"/>
<form-property name="password" type="java.lang.String"/>
</form-bean>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
* 提供validation.xml和validator_rules.xml文件,将此文件拷贝到WEB-INF下
2、validator服务器端验证
* 配置validation.xml文件
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
<form-validation>
<formset>
<form name="loginForm">
<field property="username" depends="required">
<arg key="prompt.username"/>
<arg0/><!-- 填充占位符 -->
</field>
<field property="password" depends="required,mask">
<arg key="prompt.password"/><!-- 提示消息 -->
<var>
<var-name>mask</var-name>
<var-value>^[0-9a-zA-Z]*$</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
Jsp文件
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
h1>登录(validator框架服务器端验证)</h1>
<hr>
<html:form action="login1.do">
username : <html:text property="username"/><html:errors property="username"/><br/>
password : <html:password property="password"/><html:errors property="password"/><br/>
<html:submit/>
</html:form>
</body>
3、validator客户端验证(javascript)
) * 配置validation.xml文件
* 在jsp页面中包含< html:javascript>
* 对需要验证的表单定义onsubmit事件,其中事件名称为validate+ActionForm的名称,如:validateLoginForm