Friday, November 22, 2013

Struts 2 -Repopulating a page with updated data after form submit

This post is an extension of my previous post( Creating sample Struts 2 Web Application ) that talks about creating Struts 2 web application. In this post I will talk about a much smaller and simpler trick to reload the submitted page with some updated content. I have seen on lots of forums, this question being asked that how do we show the same submitted page with new content.

 For that you need following entry as your struts.xml entry:-
<action name="loginaction" method="execute"  class="com.myorg.LoginAction">
<result name="input" >/sampleapp/Login.jsp</result>
<result name="success">/sampleapp/Login.jsp</result>
</action>


Now the entry in your Login.jsp page is, 


    <body>
              <s:form action="loginaction" method="post" > 
              <label for="name"><p>Enter your Name</p></label>
              <input type="text" size="50" name="id"/>
             <s:submit value="Submit" />
            </s:form>
      <s:if test="%{id != null}">
         <br/><p>Hello 
         <s:property value="id"/></h3>
     </s:if>

</body>


Your action class looks like this:-


public String execute() throws Exception {

if(null != id)
this.id = "Dear "+id;

        return SUCCESS;

    }


Now when you submit your page, and you have entered a name, it will be go to the action, and then repopulated on the refreshed Login page with text "Hello Dear <yourname>".


No comments:

Post a Comment