登录页面login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登录页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="login.action" method="post">
用户名:
<input type="text" name="username" size="20"
style="background-color: #FFFFEE" />
<br />
<br />
密 码:
<input type="password" name="password" size="20"
style="background-color: #FFFFEE" />
<br />
<br />
<input type="submit" value=" 登录 " size="6"
style="background-color: #FFEEDD; cursor: hand;" />
<input type="reset" value=" 取消 " size="6"
style="background-color: #FFEEDD; cursor: hand;" />
</form>
</body>
</html>
在LoginAction中:
/**
*
*/
package com.struts2.action;
import com.struts2.exception.PasswordException;
import com.struts2.exception.UsernameException;
/**
* @author 谌纪超
*
*/
public class LoginAction {
private String username;
private String password;
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username
* the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password
* the password to set
*/
public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception {
// 判断用户名
if (!"hello".equals(this.getUsername().trim())) {
throw new UsernameException("用户名不正确!");
}
// 判断密码
else if (!"world".equals(this.getPassword().trim())) {
throw new PasswordException("密码不正确!");
}
else {
return "success";
}
}
}
类UsernameException中:
/**
*
*/
package com.struts2.exception;
/**
* @author 谌纪超
*
*/
@SuppressWarnings("serial")
public class UsernameException extends Exception {
private String message;
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message
* the message to set
*/
public void setMessage(String message) {
this.message = message;
}
public UsernameException(String message) {
this.message = message;
}
}
类PasswordException中:
/**
*
*/
package com.struts2.exception;
/**
* @author 谌纪超
*
*/
@SuppressWarnings("serial")
public class PasswordException extends Exception {
private String message;
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message
* the message to set
*/
public void setMessage(String message) {
this.message = message;
}
public PasswordException(String message) {
this.message = message;
}
}
配置文件struts.xml中:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<!-- 全局页面处理 -->
<global-results>
<result name="usernameInvalid">error/usernameInvalid.jsp
</result>
<result name="passwordInvalid">error/usernameInvalid.jsp
</result>
</global-results>
<!-- 全局异常 -->
<global-exception-mappings>
<!-- 用户名异常 -->
<exception-mapping result="usernameInvalid"
exception="com.struts2.exception.UsernameException"></exception-mapping>
<!-- 密码异常 -->
<exception-mapping result="passwordInvalid"
exception="com.struts2.exception.PasswordException"></exception-mapping>
</global-exception-mappings>
<action name="login" class="com.struts2.action.LoginAction">
<!-- 局部异常 -->
<!-- 用户名异常 -->
<exception-mapping result="usernameInvalid"
exception="com.struts2.exception.UsernameException"></exception-mapping>
<!-- 密码异常 -->
<exception-mapping result="passwordInvalid"
exception="com.struts2.exception.PasswordException"></exception-mapping>
<result name="success">/result.jsp</result>
<!-- 用户名异常页面 -->
<!--
<result name="usernameInvalid">error/usernameInvalid.jsp </result>
-->
<!-- 密码异常页面 -->
<!--
<result name="passwordInvalid">error/passwordInvalid.jsp </result>
-->
</action>
</package>
</struts>
异常页面usernameValid.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用户名异常页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:property value="exception.message" />
<br />
<!-- 使用EL表达式 -->
${exception.message }
</body>
</html>
异常页面passwordValid.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>密码异常页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<s:property value="exception.message" />
<br />
<!-- 使用EL表达式 -->
${exception.message }
</body>
</html>
显示结果的页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>结果页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
用户名:${requestScope.username }
<br />
密 码:${requestScope.password }
</body>
</html>
希望给我留言,我的QQ是:897948924 欢迎来到的QQ空间!!!!