Search code examples
javajspstruts2strutsognl

Struts2 Get Data From Form Into a Separate Class


This is going to be a bit of an odd question and I'll do my best to explain it, but bear with me.

I have a .jsp page with a form to type info into, a SubmitAction.java page to handle the struts action, and a Request.java page that's really just a container for all my data (it's stripped down for the purpose of this question. In reality it contains much more data).

My main issue is that I can't get a Request object to know about any of the data that was typed into NewForm.jsp. When debugging for example, inside of the constructInsertStatement() function, the value of myTextBox is always null.

I'll post what I have and hopefully someone can tell me what I'm missing.

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>  

Submit 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 Action

<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>

Solution

  • To get a Request object you should have a getter

    public Request getMyRequest() { return request; }
    

    To set a value to this object you need valid getters and setters

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

    If you want to learn more about how OGNL accesses properties of a bean see Struts2 passing variables case answer.

    To bind this object to the textfield you should use a path to the property in the name attribute.

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