0

I am doing registration using Struts and Hibernate here and I am facing a problem like when I run my project I get

localhost/ProjectName/registration.jsp

when I submit the data, data successfully saved in database, after saving data in database I am looking for registration.jsp as response page but that time URL will be

localhost/ProjectName/actionName.action 

this would create a problem like if I refresh page the last added record will be added again

JSP Form:

<s:form action="register">
</s:form>

struts.xml:

<action name="register" class="-----------">
    <result name="success">registration.jsp</result>
</action>
Roman C
  • 49,761
  • 33
  • 66
  • 176
jancy pradeep
  • 175
  • 1
  • 3
  • 16
  • @Andrea Ligios before knowing the things please do not mark it as duplicate because many persons have good knowledge of doing work some of the peoples they may not have good communication to explain – jancy pradeep Dec 29 '16 at 05:48
  • 1
    "Before knowing the things" ? You're asking for a way to prevent double submissions, that is PRG (Post-Redirect-Get). You're using Struts2. The answer (to the question your is a duplicate of) explains exactly how to achieve PRG with Struts2. Why should I not consider this a duplicate ? – Andrea Ligios Dec 29 '16 at 09:24
  • Yes it is PRG i have tried that way but before redirecting i should show message like Registration Successful how can i do this – jancy pradeep Dec 29 '16 at 11:27
  • 1
    By Showing a message in the G page, the page you call with GET, the landing page ? No matter If it is the same page that started the original request or a different page, just add an actionMessage and show it in the page if present – Andrea Ligios Dec 29 '16 at 11:31
  • @jancy pradeep messages are in the property that you should create. Don't mess up messages from validation and errors with your own messages. – Roman C Jan 02 '17 at 20:49

1 Answers1

0

You should follow PRG pattern mentioned in this answer.

Create a new action registrationSuccess that will return your JSP page after redirect action result.

<action name="register" class="-----------">
 <interceptor-ref name="store"> 
    <param name="operationMode">STORE</param> 
  </interceptor-ref> 
  <interceptor-ref name="defaultStack" /> 
  <result name="success" type="redirectAction">registrationSuccess</result>
</action>

<action name="registrationSuccess">
  <interceptor-ref name="store"> 
    <param name="operationMode">RETRIEVE</param> 
  </interceptor-ref> 
  <interceptor-ref name="defaultStack" />
  <result name="success">registration.jsp</result>
</action>

You can use store Interceptor to store action messages/errors in HTTP session. The action class should implement ValidationAware.

The store interceptor works between two actions, one of them store the messages, another retrieve them.

An interceptor to store a ValidationAware action's messages / errors and field errors into HTTP Session, such that it will be retrievable at a later stage. This allows the action's message / errors and field errors to be available longer that just the particular HTTP request.

If no session exists, nothing will be stored and can be retrieved later. In other terms, the application is responsible to open the session.

In the STORE mode, the interceptor will store the ValidationAware action's message / errors and field errors into HTTP session.

In the RETRIEVE mode, the interceptor will retrieve the stored action's message / errors and field errors and put them back into the ValidationAware action.

In the AUTOMATIC mode, the interceptor will always retrieve the stored action's message / errors and field errors and put them back into the ValidationAware action, and after Action execution, if the Result is an instance of ServletRedirectResult, the action's message / errors and field errors into automatically be stored in the HTTP session..


Roman C
  • 49,761
  • 33
  • 66
  • 176