1

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>
Roman C
  • 49,761
  • 33
  • 66
  • 176
ZaredH
  • 491
  • 1
  • 7
  • 23

1 Answers1

2

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" />
Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I've already defined getters and setters in `Request.java`. Should they be defined elsewhere? Also, wouldn't I have to create an empty `Request` object inside the `Request` class in order to have a getter for it? – ZaredH Jun 27 '16 at 18:33
  • No, the object is created by default. – Roman C Jun 27 '16 at 18:35
  • So the getter and setter that I originally had, are they in the right location? – ZaredH Jun 27 '16 at 19:08
  • You didn't have a getter for `request`, and wrong getter and setter for `Request`'s property, but the location is right. – Roman C Jun 27 '16 at 19:48
  • That was it. Thank you so much. – ZaredH Jun 27 '16 at 19:48