web.xml下
2.struts-config下面
action 的编写
循环流程
<c:forEach>标记.
在页面中加入Struts标记的声明指令
手工表单验证:
如何使用validate框架?
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>struts.overview</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2.struts-config下面
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="logonForm" type="struts.logon.LogonFormBean" />
</form-beans>
<action-mappings>
<action path="/logon" type="struts.logon.LogonAction"
name="logonForm" scope="request">
<forward name="success" path="/success.jsp"
redirect="true" />
<forward name="error" path="/logon.jsp" />
</action>
</action-mappings>
</struts-config>
action 的编写
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LogonAction extends Action {
@Override
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception {
LogonFormBean bean = (LogonFormBean) actionForm;
if (bean.getUser().getUserName().equals("test")) {
return actionMapping.findForward("success");
}
request.setAttribute("errorMessage", "用户名或密码不正确.");
return actionMapping.findForward("error");
}
}
public class LogonFormBean extends ActionForm
1.2.1. 安装JSTL
1.2.1.1. 手工安装
加入以下jar文件到WEB-INF/lib:
jstl.jar
standard.jar
1.2.1.2. MyEclipse自动安装
在创建web项目时设置JSTL支持
追加JSTL支持
1.2.2. 在JSP页面中加入JSTL指令
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
循环流程
<c:forEach>标记.
<%
List list=new ArrayList();
list.add("中国");
list.add("美国");
list.add("日本");
request.setAttribute("countryInfos",list);
%>
<p />
国家列表:<br />
<c:forEach items="${countryInfos}" var="country">
${country }<br />
</c:forEach>
items, 引用集合类型的变量
var, 定义循环内变量, 表示集合的元素
在页面中加入Struts标记的声明指令
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
手工表单验证:
实现步骤
3.2.1.1. 表单类覆盖ActionForm的validate方法
切换行号显示切换行号显示
1 public class UserForm extends ActionForm {
2 ...
3 public ActionErrors validate(ActionMapping mapping,
4 HttpServletRequest request) {
5 // 检查用户名是否填写
6 if (this.name == null || this.name.isEmpty()) {
7 ActionErrors errors = new ActionErrors();
8 errors.add("name", new ActionMessage("error.name.required"));
9 return errors;
10 }
11 return null;
12 }
13 ...
14 }
3.2.1.2. 配置struts.xml action
在struts.xml文件中对应该Form的Action, 增加validate="true"和input="/WEB-INF/register/input.jsp"
<action name="userForm" path="/register" scope="request"
type="struts.register.manual.RegisterAction" validate="true"
input="/WEB-INF/register/input.jsp">
3.2.1.3. 配置资源文件
编辑ApplicationResources.properties文件.
该文件配置在struts.xml文件中:
<message-resources
parameter="struts.register.manual.ApplicationResources" />
使用eclipse的ResourceBundle编辑器插件打开
增加如下内容:
error.name.required = \u7528\u6237\u540D\u5FC5\u586B
errors.header = \u9519\u8BEF\u63D0\u793A
3.2.1.4. 在表单页面中加入错误提示信息
在页面中加入Struts标记的声明指令
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
使用struts标记显示错误提示信息
<logic:messagesPresent>
<fieldset class="errorMessage">
<legend><bean:message key="errors.header" /></legend>
<ul>
<html:messages id="error">
<li>
<bean:write name="error" />
</li>
</html:messages>
</ul>
</fieldset>
<p />
</logic:messagesPresent>
<logic:messagesPresent>: 显示信息, 如果没有则不显示.
<bean:message key="errors.header" />: 显示errors.header在ApplicationResources.properties中对应的信息
<html:messages id="error">: 迭代显示错误信息
<bean:write name="error" />: 显示每一个错误信息
如何使用validate框架?
创建表单类
通过eclipse创建struts 1.2的表单类, 选择superclass: org.apache.struts.validator.ValidatorForm
切换行号显示切换行号显示
1
2 public class UserForm extends ValidatorForm {
3 ...
4 private String name;
5
6 private String email;
7
8 private String birthday;
9 ...
10 }
11
5.2.1.2. 创建Action类
和以前写的一样
切换行号显示切换行号显示
1 public class RegisterAction extends Action {
2 ...
3 public ActionForward execute(ActionMapping mapping, ActionForm form,
4 HttpServletRequest request, HttpServletResponse response) {
5 UserForm userForm = (UserForm) form;
6 return mapping.findForward("success");
7 }
8 ...
9 }
5.2.1.3. 配置Action类的input属性
input="/WEB-INF/register/input.jsp"
...
<action attribute="userForm" name="userForm" path="/register"
scope="request" type="struts.register.validator.RegisterAction"
input="/WEB-INF/register/input.jsp">
...
5.2.1.4. 在表单页面中加入错误信息提示
和手工校验一样.
5.2.1.5. 编写validations.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!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="userForm">
<field property="name" depends="required">
<arg key="userForm.name" />
</field>
</form>
</formset>
</form-validation>
<form name="userForm">: 对userForm表单的验证
<field property="name" depends="required">: 对该表单下name属性检查, 要求必填(required)
<arg key="userForm.name" />: 参数键, 对应ApplicationResources.properties
errors.header = 错误提示
errors.required = {0}必填
userForm.name = 姓名
5.2.1.6. 创建ApplicationResources.propertie文件
配合validator框架, 作为提示信息使用.
errors.header = \u9519\u8BEF\u63D0\u793A
errors.required = {0}\u5FC5\u586B
userForm.name = \u59D3\u540D
5.2.2. 复杂的用户注册校验: 邮箱和日期
见示例目录下struts.register.validator.complex项目
本项目是在上面struts.register.validator.simple项目修改(增加)而成.
5.2.2.1. 在validations.xml文件增加校验设置
...
<field property="email" depends="email">
<arg key="userForm.email" />
</field>
<field property="birthday"
depends="date">
<arg key="userForm.birthday" />
<var>
<var-name>datePatternStrict</var-name>
<var-value>yyyy-MM-dd</var-value>
</var>
</field>
...
5.2.2.2. 在ApplicationResources.propertie文件增加校验设置
...
errors.date = {0}\u4E0D\u662F\u65E5\u671F
errors.email = {0}\u4E0D\u662F\u5408\u6CD5\u7684\u90AE\u4EF6\u5730\u5740
userForm.email = \u90AE\u7BB1
userForm.name = \u59D3\u540D