3

First, please note I know the following code is not in JavaScript or jQuery but I just added them as tags of this question in hope of finding a solution for it using Javascript or jQuery.

I have a form with three submit buttons. The problem is that, when I submit the form with either of the buttons the result page will be sent to the same address. I need it to be in a different address.

Purpose:

The code is supposed to show a list of items for a specific category in a page called view. User selects few items and submit the form. Server needs to show the details of the selected items in a page called update (separate address with view page). Then user is able to edit the details of items and submit the form to update the details of those items.

Requirement:

lets say address is

myexample.com/Product/view      //shows view page

after clicking on edit, it should redirect to following address.

myexample.com/Product/update     //shows update page ( show details of selected products)

Potential Solution:

Based on Roman C's answer, after receiving the request, I pass the products list to the other action to show update page, but it shows it as following on the address; therefore, update page does not recognize products parameter as a list.

...products=[1%2C+2]&id=1

Java:

List <Integer> products = new ArrayList();
   

Struts redirect:

   <result name="updateProducts" type="redirectAction">
            <param name="actionName">update</param>
            <param name="namespace">/products/Update</param>
            <param name="products">${products}</param>
            <param name="id">${id}</param>
   </result>

Even if I manually send the following request, the action class does not recognize products parameter as a list. It shows it's size is Zero.

 ...products=[1,2]&id=1
Roman C
  • 49,761
  • 33
  • 66
  • 176
Jack
  • 6,430
  • 27
  • 80
  • 151
  • 1
    See http://stackoverflow.com/q/13343954/1700321. – Aleksandr M Mar 28 '14 at 06:36
  • Do you mean that it should send to a different URL by address? – Roman C Mar 28 '14 at 08:40
  • The only possible answer is "use Ajax." The clue is "stay on myActions page": only Ajax can do that. – developerwjk Mar 28 '14 at 18:49
  • 1
    I suppose, it means when the request is made, back-end should receive the request from myAction and redirect user to a separate address from myAction. – J888 Mar 29 '14 at 23:47
  • 1
    @RomanC yes thats correct. J888 is totally right. – Jack Mar 30 '14 at 22:02
  • @AleksandrM, I followed that answer before, but it still does not work. – Jack Mar 30 '14 at 23:48
  • @developerwjk the problem is that I do not want to stay on myActions page, once a submit btn is clicked I need it to redirect to its associated address not just showing the result on myActions address. – Jack Mar 31 '14 at 00:31
  • @AleksandrM question is updated – Jack Apr 01 '14 at 05:46
  • 1
    @developerwjk question is updated – Jack Apr 01 '14 at 05:48
  • Why do you need this anyway? If for bookmarking then bookmark for update or delete makes little sense. – Aleksandr M Apr 01 '14 at 11:08
  • 1
    @developerwjk :D how did you come up with this idea? – Jack Apr 01 '14 at 23:36
  • 1
    @Jack Moore, Experience using regular html
    tag (not ) and submitting to a servlet (not JSP filled with XML tags), and then using something simple like setting a session attribute and using `response.sendRedirect("whatever.jsp"); return;` or setting a request attribute and forwarding.
    – developerwjk Apr 01 '14 at 23:58
  • @developerwjk then whats the point of using frameworks? Sessions should also be avoided because of their weaknesses. – Jack Apr 02 '14 at 00:00
  • 2
    Frameworks are great, if you don't get stuck every 10 minutes on a new "how do you do that simple thing in *this* framework" question. There's no reason to avoid sessions for stuff where sessions are appropriate. If the request is more appropriate, use a request attribute rather than session. But obviously there are cases where session is appropriate, like storing the username when the user logs in so in all pages from then on you know who this user is. – developerwjk Apr 02 '14 at 00:04
  • 1
    I'm not telling you not to use Struts, nor to abandon frameworks altogether. But from what I've seen by the questions on here, Struts seems way more complicated than Spring. – developerwjk Apr 02 '14 at 00:06
  • 1
    @developerwjk yeah maybe, I am going to learn Spring I head a lot about that. – Jack Apr 02 '14 at 00:29
  • @developerwjk Yeah, experience is great, it forms your own opinion. – Roman C Apr 02 '14 at 13:58

2 Answers2

3

You should add a redirectAction result to the action config of the action that processes a submit request. In your case it has an edit name, that is confusing, should name like save or update because when you name it edit many programmers think like you are making a request to populate some textfields. But it's not true, your edit action doesn't populate, rather than submits to the server. After submit you should follow the Post-Redirect-Get pattern and return a redirectAction or redirect result to the other action or URL not nessesary to view or edit actions if you don't want to stay on the page after redirecting. Remove the edit action config as it has covered by the wildcard mapping. Add the save button

<s:submit id="saveBtn" action="save" value="Save"/>

add and return redirect action result from the save action

<action name="save" method="save" class="myexample.Product">
  <result type="redirectAction" name="list" />
</action>   
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • alright thanks, please note items will be selected in view page, and details of selected items will be shown on update page to be updated; therefore, in this case I need to send the list of selected items or an object including them all to the new action as well, because second action should show the details of selected items to be editted, am I right? – Jack Apr 01 '14 at 05:35
  • 1
    As I got from PRG, it is to avoid duplicates. My code is supposed to show a list of items for a specific category in a page called view. User selects few items and submit the form. Server needs to show the details of the selected items in a new page called update (separate address with view page). Then user is able to edit the details of items and submit the form to update the details of those items. – Jack Apr 01 '14 at 05:44
  • Not only duplicates, when you redirect to the *new/other address* the request is cleared and you happy to fill it with the data again, not necessary it should have the same JSP that could be mapped to a different action as well. – Roman C Apr 01 '14 at 09:54
  • 1
    problem is how to send the list of selected items to the new action? question is updated. – Jack Apr 02 '14 at 04:45
  • 1
    @JackMoore The list of selected items or parameters array is sent with the request and you can get it by implementing `ParameterAware` interface see [How could I get a parameter in JSP?](http://stackoverflow.com/a/17876304/573032). – Roman C Apr 02 '14 at 13:45
2

How to redirect user to different pages based on the submit button of the form that is pressed:

URL redirection is a World Wide Web technique for making a web page available under more than one URL address. When a web browser attempts to open a URL that already has been redirected, a page with a different URL is opened.

For example, www.example.com is redirected to example.iana.org. 

Similarly, Domain redirection or domain forwarding is when all pages in a URL domain are redirected to a different domain, as when wikipedia.com and wikipedia.net are automatically redirected to wikipedia.org.

URL redirection can be used for URL shortening, to prevent broken links when web pages are moved, to allow multiple domain names belonging to the same owner to refer to a single web site, to guide navigation into and out of a website, for privacy protection, and for less innocuous purposes such as phishing attacks.

<form action="http://example.org/ jmail1.java">
<input type="submit" value="Go To jmail1.java">
</form>

<form action="http://example.org/jmail2.java">
<input type="submit" value="Go To jmail2.java">
</form>

Techniques:

Several different kinds of response to the browser will result in a redirection. These vary in whether they affect HTTP headers or HTML content. The techniques used typically depend on the role of the person implementing it and their access to different parts of the system.

For example, a web author with no control over the headers might use a Refresh meta tag whereas a web server administrator redirecting all pages on a site is more likely to use server configuration.

<action name="foo"> 
   <result type="redirectAction"> 
      <param name="actionName">bar</param> 
      <param name="namespace">/</param> 
   </result> 
</action>

Although Info path gives you the ability to create a custom message after submission, lots of users have requested more than that. They haven’t been happy with the way the form just leaves you sitting there in the List, it can be confusing. So we have a way in which you can make an InfoPath form function like a traditional web form wherein you fill out the form and upon clicking the ‘Submit’ button, the user is taken to a Thank You page

Resolution:

  1. Create a Thank You page and save it somewhere on your site. We recommend using any editor, like Notepad or SharePoint Designer to create an html page with a message of your choosing such as “Thank You for your submission”

  2. Click on that Thank You page and record the URL I.E., it will look something like this: http://mydomain.com/sites/YourSite/YourDocLibrary/ThankYou.html

  3. Now you need to figure out the URL to your InfoPath form. Either click on it and record the URL or if you have attached the InfoPath form to a SharePoint List, then you will need to alter the URL like this: http://mydomain.com/sites/YourSite/YourList/NewForm.aspx, as you can see we just added the NewForm.aspx to the end of it.

  4. Take the URL you recorded in step 3 and add the following:? Source=http://mydomain.com/sites/YourSite/YourDocLibrary/ThankYou.html, So the URL should look like this: http://mydomain.com/sites/YourSite/YourList/NewForm.aspx?Source=http://mydomain.com/sites/YourSite/YourDocLibrary/ThankYou.html << this is the link you will send to your users in order to fill out your form. When your users click on this new link, it will tell SharePoint to automatically redirect them to your Thank You page after they submit.

  5. On that Thank you page you can add a link to go to another page or you can get real fancy and add a Meta refresh tag to automatically take redirect your users, for example, to the home page of your site. How do you do that? Simple. Add this line: <meta http-equiv="refresh" content="2;url=http://mydomain.com/sites/yourSite/"> in between the tags of your HTML page. Just to explain a little more about that line, the number 2 in this case represents 2 seconds. So you can modify that to keep the user on that page longer or shorter by replacing that number with a different one. The url would be the url of your home page or any other site you want to redirect the user to.

<table><tr> <td><s:submit name="update" value="Update" action="update" ></s:submit></td> <td><s:submit name="delete" value="Delete" action="delete" ></s:submit></td> </tr></table>

<action name="update"
        class="net.viralpatel.struts2.action.MemberCrudOpertaions"
        method="updateUser">
        <result>members.jsp</result>
</action>

<action name="delete"
        class="net.viralpatel.struts2.action.MemberCrudOpertaions"
        method="deleteUser">
        <result>members.jsp</result>
</action>`

Manual redirect:

The simplest technique is to ask the visitor to follow a link to the new page, usually using an HTML anchor like:

Please follow <a href="http://www.example.com/">this link</a>.

This method is often used as a fall-back — if the browser does not support the automatic redirect, the visitor can still reach the target document by following the link.

Default redirect:

The Default redirect technique is to ask the visitor to follow a link to the new page, usually using an HTML with JSP like:

<html>
<%
String redirectURL = "http://stackoverflow.com/";
response.sendRedirect(redirectURL);
%>
</html>

http://struts.apache.org/release/2.2.x/docs/redirect-action-result.html

http://struts.apache.org/development/2.x/docs/multiple-submit-buttons.html

http://struts.apache.org/development/2.x/docs/submit.html

http://struts.apache.org/release/2.2.x/docs/interceptors.html

related source : http://en.wikipedia.org/

jmail
  • 5,944
  • 3
  • 21
  • 35