1、创建工程LoginAndRegister
2、引入Struts2的Path
3、将关键struts2-blank\lib里jar包拷贝到LoginAndRegister\WEB-INF\lib下面
4、从struts2-blank中复制web.xml和struts.xml到项目的相应目录下
注意观察是否需要修改版本号
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" 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 Blank</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>5、有一个index.jsp页面,OK,编写index.jsp
<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登陆与注册</title> </head> <body> <a href="/LoginAndRegister/login.jsp" >登陆</a><br/> <span style="white-space:pre"> </span><a href="/LoginAndRegister/register.jsp">注册</a> </body> </html></span>6、编写login.jsp
<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登陆</title> </head> <body> <span style="white-space:pre"> </span><form action="/LoginAndRegister/org/tutorial/loginAction.action" method = "post"> <span style="white-space:pre"> </span>用户名:<input type="text" name="user.name"><br/> <span style="white-space:pre"> </span>密码: <input type="text" name="user.pass"><br/> <span style="white-space:pre"> </span><input type="submit" name="登陆"> <span style="white-space:pre"> </span></form> </body> </html></span>
注意url的写法,"/"表示webapps目录,所以要写上项目名,包名和action名
tomcat收到请求,然后交给org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter,过滤器搜索struts.xml文件,搜寻对应的action,在这里搜寻loginAction.action,<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="org.tutorial" extends="struts-default"> <action name="loginAction" class="org.tutorial.loginAction"> <result name="success"> /loginSuccess.jsp </result> <result name="error"> /error.jsp </result> </action> <action name="registerAction" class="org.tutorial.registerAction"> <result name="success"> /loginSuccess.jsp </result> </action> </package> </struts></span>
7、loginAction.java
注意:action的三条规则
a、必须有一个无参构造函数
b、最好事先action接口或actionsupport接口
c、服务方法
控制器action:
<span style="font-size:18px;">package org.tutorial; import com.opensymphony.xwork2.ActionSupport; public class loginAction extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String execute(){ //System.out.println("name:pass -----" + name + ":" + pass); UserService us = new UserService(); //System.out.println("user in action:" + user); int id = us.validateUser(user); if(id == -1) return ERROR; return SUCCESS; } }</span>
<span style="font-size:18px;">package org.tutorial; public class User { private int id; private String name; private String pass; private String tele; private String addr; public User(){ } public User(int id, String name, String pass, String tele, String addr) { this.id = id; this.name = name; this.pass = pass; this.tele = tele; this.addr = addr; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public String getTele() { return tele; } public void setTele(String tele) { this.tele = tele; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((pass == null) ? 0 : pass.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (pass == null) { if (other.pass != null) return false; } else if (!pass.equals(other.pass)) return false; return true; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", pass=" + pass + ", tele=" + tele + ", addr=" + addr + "]"; } }</span>
业务处理类
<span style="font-size:18px;">package org.tutorial; import java.util.HashSet; import java.util.Set; public class UserService { //模拟用户数据库 private static Set<User> db = new HashSet<User>(); public UserService() { User u = new User(1,"wukong","123456","18898834453","beijing"); db.add(u); } public int validateUser(User user) { System.out.println("DB;" + db); System.out.println("user: " + user); // TODO Auto-generated method stub if(db.contains(user)){ return user.getId(); }else{ return -1; } } }</span>
返回视图页面loginSuccess.jsp
<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登陆成功</title> </head> <body> 欢迎您:<s:property value="user.name" /> </body> </html> </span>
8、接下来是注册模块
还是分步走
请求页面->action处理->业务逻辑
|------------>视图
请求页面register.jsp
<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title> 注册 </title> </head> <body> 欢迎使用本系统<br/> <form action = "/LoginAndRegister/org/tutorial/registerAction.action" method="post"> <table> <tr> <td>姓名</td> <td><input type="text" value="user.name"></td> </tr> <tr> <td>密码</td> <td><input type="text" value="user.name"></td> </tr> <tr> <td>联系方式</td> <td><input type="text" value="user.name"></td> </tr> <tr> <td>地址</td> <td><input type="text" value="user.name"></td> </tr> </table> </form> </body> </html> </span>
<span style="font-size:18px;">package org.tutorial; import com.opensymphony.xwork2.ActionSupport; public class registerAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String execute(){ UserService us = new UserService(); int status = us.addUser(user); if(status == -1){ return ERROR; } return SUCCESS; } } </span>
<span style="font-size:18px;">package org.tutorial; import java.util.HashSet; import java.util.Random; import java.util.Set; public class UserService { //模拟用户数据库 private static Set<User> db = new HashSet<User>(); public UserService() { User u = new User(1,"wukong","123456","18898834453","beijing"); db.add(u); } public int validateUser(User user) { System.out.println("DB;" + db); System.out.println("user: " + user); // TODO Auto-generated method stub if(db.contains(user)){ return user.getId(); }else{ return -1; } } public int addUser(User user) { // TODO Auto-generated method stub int id = new Random().nextInt(99999); user.setId(id); db.add(user); return 1; } } </span>
注册登陆都已经实现了,现在想要了解下系统中的用户
9、查询用户
分步走
页面请求---->Action------>业务
|----------->视图
1、页面请求,由于不需要发送请求参数,这里使用超链接的形式
在loginSuccess.jsp页面里添加一个超链接
<span style="font-size:18px;"><a href="/LoginAndRegister/org/tutorial/usersListAction.action">查看所有的注册用户</a></span>2、然后编写action处理类
<span style="font-size:18px;">package org.tutorial; import java.util.Set; import com.opensymphony.xwork2.ActionSupport; public class usersListAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; //为了给页面传回数据,只需要为传回的field添加属性,并提供setter和getter即可。 //如下所示,为了传回用户列表,所以定义users属性,并且提供了setter和getter方法。 private Set<User> users; public void setUsers(Set<User> users) { this.users = users; } public Set<User> getUsers() { return users; } public String execute(){ //获取users列表 UserService us = new UserService(); users = us.getUsersList(); return SUCCESS; } } </span>
<span style="font-size:18px;">public Set<User> getUsersList() { // TODO Auto-generated method stub return db; }</span>
<span style="font-size:18px;"><action name="usersListAction" class="org.tutorial.usersListAction"> <result> /usersList.jsp </result> </action></span>
设计物理视图,给用户展示
<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>用户列表</title> </head> <body> <!-- 使用标签遍历列表 --> <table width="600" border="1"> <tr> <th>姓名</th> <th>密码</th> <th>联系方式</th> <th>地址</th> </tr> <s:iterator value="users" id="u"> <tr> <td>${u.name}</td> <td>${u.pass}</td> <td>${u.tele}</td> <td>${u.addr}</td> </tr> </s:iterator> </table> </body> </html></span>
姓名 | 密码 | 联系方式 | 地址 |
---|---|---|---|
li | 1234567 | 18894385472 | shenyang |
wukong | 123456 | 18898834453 | beijing |
bai | bai | 1823484543 | shenyang |
OK,注册,登陆,查询都已完成。