jsp"/>
其中form-beans元素中可以定义0个或1个以上的form-bean元素,每个form-bean被认为是一个ActionForm对象,name属性表示form-bean元素的名称,type属性指定其类名和路径。
Action-mappings元素用来包含零到多个action,其子元素action负责具体映射的详细信息。在action-mapping元素中可以定义0个或1个以上的action元素。每个action元素接受path属性定义的请求,并映射到type属性所定义的具体action对象。在映射过程中,将name属性定义的actionform一并传过去,它有如下属性:
Parameter,scope两个属性指定了传送方式和范围,scope常用的值有两个“session”和“request”。
Validate属性指定了是否需要actionform的验证。
Forward元素,将请求success转发到”/login_success.jsp”页面。
6、 业务逻辑类UserManager和自定义异常类
代码如下所示:
packagecom.bjpowernode.struts;
publicclassUserManager {
publicvoid login(Stringusername,Stringpassword)
{
if(!"admin".equals(username))
{
thrownewUserNotFoundException();
}
if(!"admin".equals(password))
{
thrownewPasswordErrorException();
}
}
}
自定义异常类UserNotFoundException和PasswordErrorException代码如下所示。
packagecom.bjpowernode.struts;
public class UserNotFoundExceptionextends RuntimeException {
public UserNotFoundException() {
}
public UserNotFoundException(Stringmessage) {
super(message);
}
public UserNotFoundException(Throwable cause) {
super(cause);
}
public UserNotFoundException(Stringmessage,Throwable cause) {
super(message, cause);
}
}
packagecom.bjpowernode.struts;
public class PasswordErrorExceptionextends RuntimeException {
public PasswordErrorException() {
}
public PasswordErrorException(Stringmessage) {
super(message);
}
public PasswordErrorException(Throwable cause) {
super(cause);
}
public PasswordErrorException(Stringmessage,Throwable cause) {
super(message, cause);
}
}
7、 视图jsp页面调用。
登录界面login.jsp,错误显示界面login_error.jsp,登录成功界面login_success.jsp。代码如下所示。
<%@pagelanguage="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>Inserttitle here
Login_success.jsp.
<%@page language="java"contentType="text/html;charset=GB18030" pageEncoding="GB18030"%>Insert title here ${username},登录成功!
Login_error.jsp界面。
<%@page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>Insert title here <%-- <%=request.getAttribute("msg") %> --%> ${msg }
就这样我们实现了运用struts框架完成用户登录。就这样从初步学习到简单应用,随着应用的次数增多,我们会对struts理解越来越深刻,并且感受struts框架给我们带来的便捷。
下一篇Struts表单处理器ActionForm(静态动态)