Action 类
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import IllegalUserException;
import UserBiz;
import User;
/**
* Struts的Action类的编写、配置及执行流程说明。
* 编写Action: 1)继承于org.apache.struts.action.Action
* 2)覆盖execute方法
* 3)必须有缺省构造器
* 实现execute方法:
* 1)解析请求(参数、路径、头)
* 2)创建模型(业务)对象,并使用该对象处理请求
* 3)根据处理结果,返回代表下一个页面的ActionForward对象。
* 配置: 在struts-config.xml的
*
*
*
*
*
*
* path:Action的访问路径,和
*
*
* name:自定义的名称,在mapping.findForward(String name)中用。
* path:forward所代表的页面的路径
* redirect:true|false(缺省),通知ActionServlet是否使用重定向的 方式跳转到下一个页面。
* true:response.sendRedirect(forward.getPath());
* false:getRequestDispatcher(forward.getPath()).forward(request, response)
*/
public class LoginAction extends Action {
/*
* 执行的入口,同业务/模型关联。
*
* @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
* org.apache.struts.action.ActionForm,
* javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*
* @param mapping 封装了配置文件中
* 重要的方法findForward(String name)
* @param form 封装了当前请求中参数信息的一个JavaBean,要求:
* 1)继承于ActionForm
* 2)提供同请求参数相同名称的属性
* 3)运行时,系统会调用set方法把请求的参数赋给它相应的属性
* 4)系统会将刚创建的form对象保存到request或session的属性中以便复用。
* 范围 由
* 属性名 由
* @return ActionForward 封装了配置文件中,当前
* ActionServlet通过Action返回的ActionForward对象来跳转到
* 下一个页面。
*/
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionForward forward = null;
// 获取登录表单中的数据
LoginForm loginForm = (LoginForm) form;
String userName = loginForm.getUserName();
String password = loginForm.getPassword();
// 创建模型(业务)对象
UserBiz userBiz = new UserBiz();
// 执行业务方法处理请求的数据
User user = null;
try {
userBiz.find(userName, password);
} catch (IllegalUserException e) {
e.printStackTrace();
throw e;
}
// 根据业务的执行结果跳转到下一个页面
if (user != null) {
HttpSession session = request.getSession();
session.setAttribute("user", user);
forward = mapping.findForward("success");
} else {
forward = mapping.findForward("fail");
}
return forward;
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
form类 LoginForm
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
@SuppressWarnings("serial")
public class LoginForm extends ActionForm {
private String userName;
private String password;
/**
* 服务器端的校验方法
* 该方法用来校验表单中的输入信息是否满足Action执行的要求。
* 该方法是在系统调用setters之后,Action.execute之前执行。
* 如果校验失败,即返回的ActionErrors中包含错误信息,
* 系统会将返回的ActionErrors对象保存到当前请求的属性中。
* Struts提供了标签:
* 系统会转到
*
* @param mapping ActionMapping对象,同execute方法参数一样。
* @param request HttpServletRequest对象,同execute方法参数一样。
*
* @return ActionErrors 代表校验的结果,系统会根据它来决定下一步的操作。
* 1)成功,执行Action.execute方法
* 2)失败,回到输入界面重新输入信息
* 3)成功、失败的涵义
* 成功:返回值为null
* 返回的ActionErrors的isEmpty方法为true 失败:返回的ActionErrors中包含出错信息。
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (userName == null || userName.trim().length() == 0) {
//errors.username为消息文件中消息的key
//消息文件在struts-config.xml中注册
ActionMessage message = new ActionMessage("errors.username");
//参数username是ActionErrors存储message所有的key
errors.add("username", message);
}
if (password == null || password.trim().length() == 0) {
ActionMessage message = new ActionMessage("errors.password");
errors.add("password", message);
}
return errors;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
异常类 IllegalUserException
public class IllegalUserException extends Exception {
public IllegalUserException() {
}
public IllegalUserException(String arg0) {
super(arg0);
}
public IllegalUserException(Throwable arg0) {
super(arg0);
}
public IllegalUserException(String arg0, Throwable arg1) {
super(arg0, arg1);
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
struts-config.xml
path="/error.jsp" />
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ApplicationResources.properties
errors.illegaluser=you can not login as administrator.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
login.jsp
<%@page contentType="text/html;charset=gbk" %>
<%@taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<%@taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
用户名 | <%-- HTML的控件 property="":值是外层 属性名。 --%> |
密码 |
<%--
1)读取资源文件中errors.header消息并显示到页面中。
2)读取validate方法返回的ActionErrors中包含的所有的消息并显示到页面中。
3)读取资源文件中errors.footer消息并显示到页面中。
总结:将格式放到资源文件的解决方案。
--%>
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
fail.jsp
<%@page contentType="text/html;charset=gbk" %>
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
success.jsp
<%@page contentType="text/html;charset=gbk" %>
<%@taglib prefix="html" uri="http://struts.apache.org/tags-html"%>