java struts2入门学习实例--用户注册和用户登录整合

需求:

1、用户注册(user_register.jsp)--》注册成功(UserRegister.action)--》显示注册信息(register_success.jsp)
2、用户登录(user_login.jsp)--》登录成功(UserLogin.action)--》显示用户名(login_success.jsp)

分析:

这里主要涉及struts2中对于多个类似的业务操作方法的封装。

效果

针对需求1,用户注册:

java struts2入门学习实例--用户注册和用户登录整合

java struts2入门学习实例--用户注册和用户登录整合

针对需求2,用户登录:

java struts2入门学习实例--用户注册和用户登录整合

java struts2入门学习实例--用户注册和用户登录整合

实现:

user_register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="register" action="/struts2/UserRegister" method="post">
<table border="2" align="center">
<caption>新用户注册</caption>
<tr>
<th>用户名:</th>
<td><input name="username" id="username" type="text" /></td>
</tr>
<tr>
<th>密码:</th>
<td><input name="password" id="password" type="password" /></td>
</tr> <tr>
<td colspan="2" align="center"><input type="submit" value="提交"
width="120ppx" /></td>
</tr> </table>
</form>
</body>
</html>

user_login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="register" action="/struts2/UserLogin" method="post">
<table border="2" align="center">
<caption>用户登录</caption>
<tr>
<th>用户名:</th>
<td><input name="username" id="username" type="text" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"
width="120ppx" /></td>
</tr> </table>
</form>
</body>
</html>

UserAction.java

package com.amos.web.action;

import com.opensymphony.xwork2.ActionSupport;

/**
* @ClassName: UserAction
* @Description: 用户管理,将相关的action封装到一个类中
* @author: amosli
* @email:amosli@infomorrow.com
* @date Jan 8, 2014 1:06:00 AM
*/
public class UserAction extends ActionSupport {
private static final long serialVersionUID = -6275534406709255984L;
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;
} // 用户注册
public String register() throws Exception { return "toRegisterJsp";
} // 用户登录
public String login() throws Exception {
return "toLoginJsp";
} }

struts.xml

<?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>
<!-- 加载其他配置文件 -->
<include file="config/user_struts.xml"></include>
</struts>

user_struts.xml

<?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="user" namespace="/" extends="struts-default">
<action name="UserRegister" class="com.amos.web.action.UserAction"
method="register">
<result name="toRegisterJsp" type="dispatcher">
/register_success.jsp
</result>
</action>
<action name="UserLogin" class="com.amos.web.action.UserAction"
method="login">
<result name="toLoginJsp" type="dispatcher">
/login_success.jsp
</result>
</action>
</package>
</struts>

login_success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登录成功!
<hr>
用户名:<s:property value="username"/>
</body>
</html>

register_success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
注册成功<br/>
<hr>
用户名:<s:property value="username" />
密码:<s:property value="password"/>
</body>
</html>

代码分析:

代码的入口为两个jsp程序,user_register.jsp和user_login.jsp,因为原理大体相同,这里主要讲一下登录,注册上一篇文章已经讲过了。

首先,在地址栏里输入http://localhost:8080/struts2/user_login.jsp,表单的action动作为POST方式提交,对应的动作时是UserLogin.action;UserLogin在struts启动时struts.xml加载到内存中时已经将user_struts.xml 反射出来了。user_struts.xml中已经配置了,调用com.amos.web.action.UserAction中的login方法,然后将值再转发到login_success.jsp中,login_success.jsp,通过s标签进行取值并最终显示到浏览器中。

这里最主要的思想在于将登录和注册这两种相类似的业务,相关联的业务整合到一个action中。

户注册(user_register.jsp)--》注册成功(UserRegister.action)--》显示注册信息(register_success.jsp)

用户登录(user_login.jsp)--》登录成功(UserLogin.action)--》显示用户名(login_success.jsp)

上一篇:java struts2入门学习实例--用户注册


下一篇:Java中的设计模式