网上说在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>
请大侠指点
<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>
请大侠指点
解决方案 »
- 为什么要用final
- 一个正则表达式的问题、、、
- action中文乱码 请高手帮忙解决
- 数据库访问问题
- 用tomcat运行jsp文件出错
- xml绑定到select控件中问题
- 各位,那里有ibatis和oracle的连接实例?
- 如何用弹出文本框的方式将一个字段的内容保存到数据库?
- 关于JBoss和hello.war?(不好意思我只有10分)
- HTTP Status 500 - /front/zyfy/zyfymx.jsp(61,3) The function size must be used wi
- java 实现房间比赛的问题 类似QQ斗地主,但是在网页内实现
- 如何根据文本框输入的数字,动态的添加文本框
用户名:<input type="text" name="t.temail"><br>
密码:<input type="text" name="t.tpass"><br>
<input type="submit" value="登录">
</form>
tpass = t.getTpass();
其实有两种方法:
第一种选择也是基于JavaBean。可以公开复杂的对象作为JavaBean属性,让数据直接传到这个对象上;另一种选择是被称为ModelDriven的动作。这种选择涉及到一个简单的接口,和另一个默认的拦截器。
默认调用的是 excute 函数,而不是 login。
具体怎么实现?JavaBean那种 ..
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="login" method="login" class="com.mulan.struts.action.Bacis">
<result name="success">/register.jsp</result>
</action>
<!-- action name="register" method="register" class="com.mulan.struts.action.LoginAction">
<result name="success"></result>
</action> -->
</package>
</struts>
private String tpass;
public void setTemail(String temail) {
this.temail = temail;
}
public String getTemail() {
return this.temail;
} public String login() throws Exception{
String temail = null;
String tpass = null;
//获得前台数据
try {
temail = this.temail; //前台form输入的值可以获取
tpass = this.tpass;
} catch ( NullPointerException npe ) {
npe.printStackTrace();
}
}
2.你也可能在action的方法中
String name = request.getParameter("name");就可以取到值了。
用户名:<input type="text" name="t.temail"><br>
密码:<input type="text" name="t.tpass"><br>
这个获取方式吗 如果是的话应该没用问题的
public void setTreasure(Treasure t) {
this.t = t;
}
public Treasure getTreasure() {
return t;
}
我想你的get方法有问题。
我记得java反射机制后,它会去找getXXXX()方法的get之后的名字作为字段名,就是XXX.
我猜可能是这样
重新生成一下get set方法,或者直接把t重命名为treasure.
public void setT(Treasure t) {
this.t = t;
}
public Treasure getT() {
return t;
}
private Treasure t = new Treasure(); //实例化
public void setTreasure(Treasure t) {
this.t = t;
}
public Treasure getTreasure() {
return t;
}
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;
}
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 = this.temail(); //这里想获得前台form的值 可是为空
tpass = this.tpass();
} catch ( NullPointerException npe ) {
npe.printStackTrace();
}
return SUCCESS;
}
}
试试这个,Form中的信息貌似是直接传给action中的同名属性的。我也是新手,错了的话不要见怪。