1.创建如下的目录结构
2.在com.entity包下创建
package com.entity;
/**
* 用户类
* @author Holly老师
*
*/
public class User {
private String username; //用户名
private String password; //密码
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }
User.java
3.在com.action包下创建LoginAction.java
package com.action; import com.entity.User;
import com.opensymphony.xwork2.Action;
/**
* 登陆控制类
* @author Holly老师
*
*/
public class LoginAction implements Action {
/**
* action注入实体类对象
* 该对象名和表单里的name属性值的前缀保持一致
* 否则注入不了
*/
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
} public String execute() throws Exception {
if(user.getUsername().equals("holly") && user.getPassword().equals("123")){
return "success";
}else{
return "error";
} } // private String user;
// private String pwd;
// public String getUser() {
// return user;
// }
//
// public void setUser(String user) {
// this.user = user;
// }
//
// public String getPwd() {
// return pwd;
// }
//
// public void setPwd(String pwd) {
// this.pwd = pwd;
// }
// public String execute() throws Exception {
// if(user.equals("holly") && pwd.equals("123")){
// return "success";
// }else{
// return "error";
// }
//
// } }
LoginAction.java
4.在src跟目录下创建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="default" namespace="/" extends="struts-default">
<action name="login" class="com.action.LoginAction">
<result name="success">success.jsp</result>
<result name="error">fail.jsp</result>
</action>
</package>
</struts>
struts.xml
5.在WebRoot下编辑index.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<%
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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'index.jsp' starting page</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" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
</head> <body> <form action="login.action" method="post">
<table>
<tr> <td>
用户名:
</td>
<td>
<input type="text" name="user.username" />
</td>
</tr>
<tr> <td>
密码:
</td>
<td>
<input type="password" name="user.password" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>
index.jsp
6.在WebRoot下创建success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'index.jsp' starting page</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" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
</head> <body>
<h1>登录成功</h1>
欢迎您,<s:property value="user.username"/>
</body>
</html>
success.jsp
7.在webRoot下创建fail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'index.jsp' starting page</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" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
</head> <body>
<h1>登录失败</h1>
用户名或密码错误!
</body>
</html>
fail.jsp