网上说在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>
请大侠指点

解决方案 »

  1.   

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

  2.   

    Quote: 引用 1 楼 forgetsam 的回复:

    <form action="login.action" method="POST">           用户名:<input type="text" name="t.temail"><br>           密码:<input type="text" name="t.tpass"><br>           <input type="submit" value="登录"> </form>
    Quote:
    这我试过了 不行 .. 
      

  3.   

     temail = t.getTemail(); //这里想获得前台form的值 可是为空
       tpass = t.getTpass();
      

  4.   

    getTreasure()这个东西获取的是null把!
    其实有两种方法:
    第一种选择也是基于JavaBean。可以公开复杂的对象作为JavaBean属性,让数据直接传到这个对象上;另一种选择是被称为ModelDriven的动作。这种选择涉及到一个简单的接口,和另一个默认的拦截器。
      

  5.   

    代码是没有问题的,把你struts配置贴出来。
    默认调用的是 excute 函数,而不是 login。
      

  6.   


    具体怎么实现?JavaBean那种 .. 
      

  7.   

    struts的配置文件是这样的 我断点调试过 确实走的是login .. <?xml version="1.0" encoding="UTF-8" ?>
    <!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>
      

  8.   

    而且我这样写 确实是可以获得值的 不过是在同一个类中 .. 只是想把javaBean和获取值分开 ..          private String temail;
    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();
    }
              }
      

  9.   

    1.你在action中把页面的input中的name值设为全局变量,并get\set,到方法中直接用就可以了。
    2.你也可能在action的方法中
    String name = request.getParameter("name");就可以取到值了。
      

  10.   

    getTreasure()这方法返回的t是个null,应该给实例化个t
      

  11.   

    如果是报空你前台获取数据 用是
    用户名:<input type="text" name="t.temail"><br>          
    密码:<input type="text" name="t.tpass"><br>
    这个获取方式吗 如果是的话应该没用问题的 
      

  12.   

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

    我想你的get方法有问题。
    我记得java反射机制后,它会去找getXXXX()方法的get之后的名字作为字段名,就是XXX.
      

  13.   

    继楼上,你的变量名是t,struts通过反射找到的变量是treasure,当然找不到。
    我猜可能是这样
    重新生成一下get set方法,或者直接把t重命名为treasure. 
      

  14.   

    代码改成
    public void setT(Treasure t) {
            this.t = t;
        }
        public Treasure getT() {
            return t;
        } 
      

  15.   

    Verification vf = new Verification();
         
        private Treasure t = new Treasure(); //实例化
        public void setTreasure(Treasure t) {
            this.t = t;
        }
        public Treasure getTreasure() {
            return t;
        }
      

  16.   

    如果你用的是Struts2框架,在action中的login方法中定义表单属性,只要你的action中的属性名和form表单的name="tpass"一样,struts2就会自动给你action中的属性复制,就不需要get(),和set()了!但是必须生成get(),和set()方法
      

  17.   

    public class Bacis extends ActionSupport {
        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中的同名属性的。我也是新手,错了的话不要见怪。