Вопрос

I tried searching the site but could not find an answer to my question.

i am developing a java Struts2 web application. There are 3 form fields on a jsp page as follows: (Struts2 tags are being used)

<s:form action="action1">

other fields
......
<s:select name="test1" list="{'A','B','C'}"></s:select>
<s:textfield name="test2"></s:textfield>
<s:textfield name="test3"></s:textfield>
.....
other fields
<s:submit value="submit1"><s/submit>
</s:form>

when a value is selected in test1 field, test2 and test3 would needed to be populated from the database based on the value selected in test1.

as per the process i need to implement, i need to do some calculations based on the input from jsp1(presented above), and present the result on jsp2 which has to have entirely different content from jsp1. My issue is limited to the data entry in jsp1.

what would be the best way of doing this without javascript? assume that javascript is disabled in the browsers accessing the application.

thanks,

Это было полезно?

Решение

EDIT

It seems that there's a bit of confusion here, let's try to make it clear:

There are basically three ways of triggering a communication with the server from the browser:

  1. submit HTML
  2. submit JS
  3. AJAX submit

You may or may not care about giving support to users browsing with JavaScript disabled;

  • if you DO NOT care, then you can proceed as you wish;
  • if you DO care, then you have two ways in front of you:
    • make an unique version of the pages, that works both with and without JS (by using ONLY option "1", "submit HTML");
    • make the pages working in two possible ways, mutually exclusive: while processing the page, you detect if the user has javascript enabled: if yes, you go with JS (submit or AJAX), if not, you fallback to the non JS solution ( "submit HTML" ).

Both this two solutions works with and without JS, but the latter is generally preferred because you can set up a nice, good-looking, user's experience-oriented WebApp for the 99% of the users, by using JavaScript and eventually AJAX, and create a fallback solution for the 1% of the users that, even if the site won't be nice as in the JS version, and even if it won't have ALL the features of the JS version, it would still be usable, and the core functionalities will be available.

As I said in the comment above, there is no need for the fallback version of the WebApp to be as nice, as fast, as good in user experience as the JS version: it should simply... work.

For example, this JSP will work in both cases: it will do a JavaScript Submit after selecting an element from the Select if JS is enabled, and it will do a submit after pressing the Submit button if JS is disabled.

With JS disabled, onchange will be ignored and <noscript> processed.
With JS enabled, onchange will be processed and <noscript> ignored.

<s:form action="myAction">
    <s:select    onchange="javascript:document.forms[0].submit();" 
                 name="test1" value="test1" list="{'A','B','C'}"  />
    <s:textfield name="test2" value="test2" />

    <noscript>
        <span>
            Since you have JS disabled, 
            you need to manually press to the GO button, 
            but you still can make it work ;)
        </span>
        <s:submit    value="go" />
    </noscript>

</s:form>

in your Action

public class MyAction extends ActionSupport{

    private String test1="";
    private String test2;
    /* Getters and Setters */       

    public String execute(){
        if (test1.length()>0)
            assignValues();
        return SUCCESS;
    }

    private void assignValues(){
        if (test1.equals("A")){
            test2 = "A was chosen, do something";
        } else if (test1.equals("B")){
            test2 = "B was chosen, do something else";
        } else if (test1.equals("C")){
            test2 = "C was chosen, what's next?";
        }
    }
}

The other doubts you are expressing in comments suggest that you may want to step back for a moment and read some Struts2 tutorial, to be sure of gaining the maximum from the framework.

If you have other fields in the same Form that you don't want to be affected, just declare a variable in the Action (with the Getter and the Setter), for each one of them: they will be preserved in the reloaded page, because they will be sent (because they're in form) with the submit, they will be injected through the Setter, they will be read back through the Getter and injected in the new page by the matching with their name and the Action variable.

Otherwise you could use AJAX, but I'd start from this.

And no, you can't nest forms.

Другие советы

Thanks to Andrea Ligios, i have the below solution to my issue.

jsp1 was changed as below

<s:form action="action2">

other fields
......
<s:select name="test1" list="{'Select','A','B','C'}" 
    onchange="javascript:document.forms[0].submit();"></s:select>
<noscript><s:submit value="populate test2 and test3"></s:submit></noscript>
<s:textfield name="test2"></s:textfield>
<s:textfield name="test3"></s:textfield>
.....
other fields
<s:submit value="submit1" action="action1"><s/submit>
</s:form>

struts.xml has following mappings

.....
<action name="action2" class="MyAction" method="populate">
    <result name="success">/jsp1.jsp</result>
</action>
<action name="action1" class="MyAction">
    <result name="success">/jsp2.jsp</result>
</action>
.....

MyAction has following code

public class MyAction extends ActionSupport{

    //all field declarations

    //Getters and Setters

    public String execute(){

    //do processing for jsp2 based on values from jsp1

    return SUCCESS;
    }

    public String populate(){

    //populate test2 and test3 from database based on value of test1

    return SUCCESS;
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top