Struts2中action通过model获取表单数据

//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方法来给实体赋值。

转载于:https://my.oschina.net/kaixuan1995/blog/608214

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值