//Action层
public class UserAction extends ActionSupport{
private User user;
private UserService userservice;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String adduser(){
userservice = new UserService();
userservice.adduser(user);
System.out.println(user+"22222");
return "addsuccess";
}
}
//实体类
package com.sharebook.model.po;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_user")
public class User {
private int user_id;
private String username;
private String password;
private String sex;
private String achievement;
private String e_mail;
private String address;
private String profession;
private String school_name;
private int user_type;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id", unique = true, nullable = false)
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAchievement() {
return achievement;
}
public void setAchievement(String achievement) {
this.achievement = achievement;
}
public String getE_mail() {
return e_mail;
}
public void setE_mail(String e_mail) {
this.e_mail = e_mail;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
public String getSchool_name() {
return school_name;
}
public void setSchool_name(String school_name) {
this.school_name = school_name;
}
public int getUser_type() {
return user_type;
}
public void setUser_type(int user_type) {
this.user_type = user_type;
}
@Override
public String toString() {
return "User [user_id=" + user_id + ", username=" + username + ", password="
+ password + ", sex=" + sex + ", achievement=" + achievement
+ ", e_mail=" + e_mail + ", address=" + address
+ ", profession=" + profession + ", school_name=" + school_name
+ ", user_type=" + user_type + "]";
}
}
//jsp摘要
<form action="<%=basePath%>user/userregist!adduser" method="post">
<table width="100%" align="center">
<tr>
<td width="41%"><div align="right">
<span class="STYLE5">账号:</span>
</div></td>
<td width="59%">
<label> <input name="user.e_mail" type="text" class="STYLE6"
onfocus="if(value=='电子邮箱'){value='';};"
onblur="if(value==''){value='电子邮箱'; };" value="电子邮箱"/ ></label>
</td>
</tr>
<tr>
<td class="STYLE5"><div align="right">密码:</div></td>
<td>
<label> <input name="user.password" type="password"
class="STYLE5" />
</label>
</td>
</tr>
<tr>
<td>
<label></label>
<div align="right">
<span class="STYLE5">确认密码: </span>
</div>
</td>
<td>
<label> <input name="repassword" type="password"
class="STYLE5" />
</label>
</td>
</tr>
</table>
注意表单中的属性命名一定要是:user.e_mail 、user.username 、user.password这类格式。否则表单数据后台的user实体接收不到,同时action中是通过实体user的getUser方法来给实体赋值。