【问题标题】:Struts2 Get Data From Form Into a Separate ClassStruts2 从表单中获取数据到一个单独的类中
【发布时间】:2016-06-27 17:48:44
【问题描述】:

这将是一个有点奇怪的问题,我会尽力解释它,但请耐心等待。

我有一个 .jsp 页面,其中有一个用于输入信息的表单,一个 SubmitAction.java 页面来处理 struts 操作,以及一个 Request.java 页面,它实际上只是我所有数据的容器(为此目的已将其剥离这个问题。实际上它包含更多的数据)。

我的主要问题是我无法让 Request 对象了解输入到 NewForm.jsp 中的任何数据。例如在调试时,在constructInsertStatement() 函数内部,myTextBox 的值始终为null。

我会发布我所拥有的,希望有人能告诉我我缺少什么。

NewForm.jsp

<html>
<head>
<sx:head />

</head>
<body>

    <s:form action="submitNew" method="post" namespace="/">
        <s:textfield label="Text Box" name="myTextBox" 
        <s:submit label="Submit" name="submit_btn" align="center" />
    </s:form>
</body>
</html>  

提交 Action.java

    public class SubmitAction extends ActionSupport {

    private Request request = new Request();

    public void executeInsert() throws SQLException{
        Connection conn = null;
        PreparedStatement ps = null;       

        try {    
            ps = request.constructInsertStatement();

            // execute the INSERT Statement
            ps.executeUpdate();
        } catch (SQLException e) {

            System.out.println(e.getMessage());

        } catch (Exception e) {

            System.out.println(e.getMessage());

        }finally {

            if (ps != null) {
                ps.close();
            }

            if (conn != null) {
                conn.close();
            }
        }
    }
}

Request.java

public class Request {
    private String myTextBox;

    public PreparedStatement constructInsertStatement() throws SQLException{
        PreparedStatement ps = null;
        Connection conn = null;  

        String URL = "myURL";

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        conn = DriverManager.getConnection(URL, "defaultUser", "defaultPassword");

        String sql = "INSERT INTO `myTable` (SomeText)"; 
        sql += "VALUES";
        sql+="(?)";

        ps = conn.prepareStatement(sql);

        try{
            ps.setString(1, myTextBox);

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        } catch (Exception e) {

            System.out.println(e.getMessage());

        }

        return ps;
    }

    public String getmyTextBox() {
        return myTextBox;
    }

    public void setmyTextBox(String myTextBox) {
        this.myTextBox = myTextBox;
    }

Struts.xml 动作

<struts> 
    <package name="default" extends="struts-default" namespace="/">        
        <action name="submitNew"
            class="my.package.path.SubmitAction" method="executeInsert">
            <result name="success">NewForm.jsp</result>
        </action>
    </package>
</struts>

【问题讨论】:

    标签: java jsp struts2 struts ognl


    【解决方案1】:

    要获得 Request 对象,您应该有一个 getter

    public Request getMyRequest() { return request; }
    

    要为该对象设置值,您需要有效的 getter 和 setter

    public String getMyTextBox() {
        return myTextBox;
    }
    
    public void setMyTextBox(String myTextBox) {
        this.myTextBox = myTextBox;
    }
    

    如果您想了解有关 OGNL 如何访问 bean 属性的更多信息,请参阅Struts2 passing variables case 答案。

    要将此对象绑定到文本字段,您应该在 name 属性中使用属性的路径。

    <s:textfield label="Text Box" name="myRequest.myTextBox" />
    

    【讨论】:

    • 我已经在Request.java 中定义了getter 和setter。他们应该在别处定义吗?另外,我是否必须在 Request 类中创建一个空的 Request 对象才能获得它?
    • 不,对象是默认创建的。
    • 那么我最初拥有的 getter 和 setter,它们的位置是否正确?
    • 您没有request 的getter,并且Request 的属性的getter 和setter 错误,但位置正确。
    • 就是这样。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多