Question

How is it possible to set the target of an <sj:submit> to top.

As an sample login page should open new AFTER the ajax base validations ends:

<s:form id="loginForm" action="login-process-result" namespace="/security" >

    <sj:submit button="true" 
        validateFunction="formValidator" validate="true" key="form.btn.login    />
</s:form>

I tried sj:submit targets and s:form target, none of the worked!

I do not want to use s:submit as the form validation must be done via Ajax.

Was it helpful?

Solution 3

To follow the struts2 jquery ajax validation and target concept, I used below solution:

The login-form.jsp (no target is set):

<s:form id="loginForm" action="login-process-result" namespace="/security" >
    <sj:submit button="true" 
        validateFunction="formValidator" validate="true" key="form.btn.login    />
</s:form>

The action (with validations)

  @Action(value = "login-process-result")
  @Validations(requiredStrings={
          @RequiredStringValidator(fieldName="userID",key="validate.required"),
          @RequiredStringValidator(fieldName="password",key="validate.required")} )
  public String result() throws ClientException{

           //Authentication login goes here  
           return SUCCESS;
  }

And finally the result form which do the trick, is a normal jsp which changes the window location via javascript

The page shows an success message and make an small delay before changing the url, which is optional and can be removed.

<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

You have logged in successfully .... 
<br/>
<img src="images/loggin-in-progress.gif" />
<s:url var="url" action="private-page" namespace="/"/>

<script type="text/javascript">
    function reload() {
        window.location.href = "${url}";
    }

    $(document).ready(function() {
        setTimeout('reload()', 800);
    });

</script>

OTHER TIPS

Put it on the form, not on the submit tag:

<s:form action = "login-process-result" 
     namespace = "/security" 
            id = "loginForm" 
        target = "_top" >

But are you sure this is what you want ? Take a look at how target="_top" works.

From your description, I don't think so.

I have no idea why you require such a behavior.

All I can imagine, is just updating a particular part of a page via Ajax using the struts2 jquery plugin to submit a form & get the response and push it in the DOM.

Here's what you can do :

  1. Submit the form via ajax
  2. Do validation on the server-side
  3. Make the response being returned fill in the desired area of the page.

In order to do this using the Struts2 Jquery Plugin, you can use onCompleteTopics & onErrorTopics of sj:submit

JSP

<s:form action="myAction">
<!-- form elements -->
<sj:submit onCompleteTopics="success" onErrorTopics="error"/>
</s:form>

Javascript

$.subscribe('success',function(event){
    var responseText=event.originalEvent.responseText;
});

Above is un-tested code, but I'm sure it explains on how to attend the problem in hand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top