日期:2014-05-18  浏览次数:20577 次

struts2 + hibernate 怎么获得前台form中的值
    网上说在action中定义一个和前台的name属性值 相同的属性 如:
    <input type="text" name="username"/>
    在action中就定义
    String username; 并且设置其的set get 方法

    可是当我用了hibernate的反向工程以后 就自己生成了set get方法 只是和action不在同一个类中 在官网上看到一些例子 就尝试了一下 

反向工程后的get set 方法

public class Treasure implements java.io.Serializable {

// Fields

private String temail;
private String tpass;

public Treasure() {
}

         public String getTemail() {
return this.temail;
}

public void setTemail(String temail) {
this.temail = temail;
}

public String getTpass() {
return this.tpass;
}

public void setTpass(String tpass) {
this.tpass = tpass;
}
}



在action中

public class Bacis extends ActionSupport {

private static final long serialVersionUID = 1L;

Connection con = null;
Statement stmt = null;
ResultSet rs = null;
DateSource ds = null;

Verification vf = new Verification();

private Treasure t;    //这里是实力了上面那个类 然后下面是get set这个类 我看官网上是这么写的 不过不知道我领悟的对不对

public void setTreasure(Treasure t) {
this.t = t;
}
public Treasure getTreasure() {
return t;
}

public String execute() throws Exception{
return null;
}

/**
 * 登录(程序是走了这个方法 .. )
 */
public String login() throws Exception{

String temail = null;
String tpass = null;

/**
 * 链接数据池
 */
ds = new DateSource();
try {
con = ds.getDate();
} catch ( Exception e ) {
e.printStackTrace();
}

//获得前台数据
try {
temail = getTreasure().getTemail(); //这里想获得前台form的值 可是为空
tpass = getTreasure().getTpass();
} catch ( NullPointerException npe ) {
npe.printStackTrace();
}
return SUCCESS;
}
}



在JSP中

<form action="login.action" method="POST">
   用户名:<input type="text" name="temail"><br>
   密码:<input type="text" name="tpass"><br>
   <input type="submit" value="登录">
</form>


请大侠指点


------解决方案--------------------
<form action="login.action" method="POST">
          用户名:<input type="text" name="t.temail"><br>