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>
<!-- 指定打开.jsp页面默认编码 -->
<constant name="struts.i18n.encoding" value="UTF-8" />

<package name="registration" namespace="/bjsxt" extends="struts-default">
<action name="user_*" class="com.bjsxt.registration.action.UserAction"
method="{1}">
<result name="SUCCESS" type="redirect">/registSuccess.jsp</result>
<result name="FAIL" type="redirect">/registFail.jsp</result>
</action>
</package>

</struts>


UserAction.java

package com.bjsxt.registration.action;

import com.bjsxt.registration.model.User;
import com.bjsxt.registration.service.UserManager;
import com.bjsxt.registration.service.impl.UserManagerImpl;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class UserAction extends ActionSupport {
private String username;
private String password;
private String password2;

private UserManager um = new UserManagerImpl();

public String getPassword2() {
return password2;
}

public void setPassword2(String password2) {
this.password2 = password2;
}

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 UserManager getUm() {
return um;
}

public void setUm(UserManager um) {
this.um = um;
}

@Override
public String execute() throws Exception {
User user = new User();
user.setUsername(username);
user.setpassword(password);
if (um.exists(user)) {
return "FAIL";
}
um.add(user);
return "SUCCESS";
}
}

regist.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>用户注册</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">

</head>

<body>
<h1>用户注册</h1><br/>
<form action="<%=request.getContextPath() %>/bjsxt/user_execute.action" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
确认密码:<input type="password" name="password2"/><br/>
<input type="submit" value=" 提 交 "/>
</form>
</body>
</html>