日期:2014-05-17  浏览次数:20911 次

大侠、高手请进!!
index.jsp代码:
<%@ page language = "java" import = "java.util.*" pageEncoding ="GB18030"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<html>
<head>
<title>用户注册</title>
</head>
<body>
<s:form action="user1.action" method="post">
  id:<s:textfield name="user.name"></s:textfield></br>
  pw:<s:password name="user.password"></s:password>
  <s:submit value="提交"></s:submit>
</s:form>
</body>
</html>

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="user" class ="com.bjsxt.hibernate.action.UserAction">
  <result name="success">  
  /registerSuccess.jsp
  </result>
  <result name="fail">registerFail.jsp</result>
  </action>
</struts>

UserAction.java代码:
import java.util.Map;
import org.apache.struts2.ServletActionContext;

import com.bjsxt.hibernate.model.User;
import com.bjsxt.hibernate.service.UserManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class User1Action extends ActionSupport{
private User user ;
private UserManager um ;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserManager getUm() {
return um;
}
public void setUm(UserManager um) {
this.um = um;
}
@Override
public String execute()throws Exception{

user = new User();
um = new UserManager();
user.setName("b");
user.setPassword("b");
if(um.exists(user)){
return "fail";
}
um.add(user);
return "success";

}

}


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

<html>
<head>
<title></title>
</head>
<body>
<h1>success!!!</h1>
</body>
</html>

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

<html>
<head>
<title></title>
</head>
<body>
<h1>fail!!!</h1>
</body>
</html>


User.java代码:
package com.bjsxt.hibernate.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;


@Entity
public class User
{
private int id ;

private String name;
private String password;

public User(){}
public void setId(int id){
this.id = id;
}
public void setName(String name){
this.name = name;
}


public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Id