简体   繁体   English

Struts2:基于下拉值自动填充字段,而无需JavaScript

[英]Struts2: Autopopulating fields based on a dropdown value without JavaScript

I tried searching the site but could not find an answer to my question. 我尝试搜索该站点,但找不到我的问题的答案。

i am developing a java Struts2 web application. 我正在开发Java Struts2 Web应用程序。 There are 3 form fields on a jsp page as follows: (Struts2 tags are being used) jsp页面上有3个表单字段,如下所示:(正在使用Struts2标签)

<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. 当在test1字段中选择一个值时,将需要根据test1中选择的值从数据库中填充test2和test3。

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. 根据我需要实现的过程,我需要基于jsp1(上面显示)的输入进行一些计算,并将结果显示在jsp2上,该内容必须与jsp1完全不同。 My issue is limited to the data entry in jsp1. 我的问题仅限于jsp1中的数据输入。

what would be the best way of doing this without javascript? 没有JavaScript,这样做的最佳方法是什么? assume that javascript is disabled in the browsers accessing the application. 假设在访问应用程序的浏览器中禁用了javascript。

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 提交HTML
  2. submit JS 提交JS
  3. AJAX submit AJAX提交

You may or may not care about giving support to users browsing with JavaScript disabled; 可能会可能不会在乎为禁用JavaScript的用户提供支持;

  • 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"); 制作页面的唯一版本,无论是否使用JS都可以使用该页面(通过使用选项“ 1”,“提交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" ). 使页面以两种可能的方式工作,相互排斥:在处理页面时,您会检测用户是否启用了javascript:如果是,则使用JS(提交或AJAX),如果否,则退回到非JS解决方案( “提交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. 这两种解决方案都可以使用JS,也可以不使用JS,但后者通常是首选,因为您可以使用JavaScript以及最终使用AJAX为99%的用户设置一个美观,美观的用户体验型WebApp。一个针对1%的用户的后备解决方案,即使该网站不能像JS版本那样好,即使它不具有JS版本的所有功能,它仍然可以使用,并且核心功能将可用。

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. 正如我在上面的评论中所说,WebApp的后备版本不需要像JS版本一样好,一样快,在用户体验方面一样好:它应该...可以工作。

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. 例如,此JSP在这两种情况下都可以工作:如果启用了JS,则从Select中选择一个元素后,它将执行JavaScript提交;如果禁用了JS,它将在按Submit按钮后执行提交。

With JS disabled, onchange will be ignored and <noscript> processed. 在禁用JS的情况下,onchange将被忽略并进行<noscript>处理。
With JS enabled, onchange will be processed and <noscript> ignored. 启用JS后,将处理onchange并忽略<noscript>

<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. 您在评论中表达的其他疑问表明您可能需要退后一会并阅读一些Struts2教程,以确保从框架中获得最大收益。

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. 如果您不想影响同一表单中的其他字段,只需在Action(使用Getter和Setter)中声明一个变量,即可针对每个变量进行声明:它们将保留在重新加载的页面中,因为它们将与提交一起发送(因为它们采用表单形式),所以它们将通过Setter注入,它们将通过Getter读回,并通过与它们的名称和Action变量的匹配注入到新页面中。

Otherwise you could use AJAX, but I'd start from this. 否则,您可以使用AJAX,但是我将从这里开始。

And no, you can't nest forms. 不,您不能嵌套表格。

Thanks to Andrea Ligios , i have the below solution to my issue. 感谢Andrea Ligios ,我对我的问题有以下解决方案。

jsp1 was changed as below jsp1更改如下

<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 struts.xml具有以下映射

.....
<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 MyAction具有以下代码

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM