简体   繁体   English

将超链接中的动态字符串作为jsp中的参数传递

[英]Passing dynamic string in hyperlink as parameter in jsp

I am trying to pass a dynamic string builder variable in jsp I am generating a string through code. 我试图在jsp中传递动态字符串生成器变量,我正在通过代码生成字符串。 String Builder variable has some value but i am not able to pass it in at run time.It doesn't get the value. 字符串生成器变量具有一些值,但我无法在运行时将其传递给它。 CODE FOR VARIABLE 变量代码

<% 
    StringBuilder sb = new StringBuilder("");
    if (request.getAttribute("Brand") != null) {
        String Brand[] = (String[]) request.getAttribute("Brand");
        for (String brand : Brand) {
            sb.append("Brand=");
            sb.append(brand);
            sb.append("&");
        }
    }
    if (request.getAttribute("Flavour") != null) {
        String Flavour[] = (String[]) request.getAttribute("Flavour");
        for (String flavour : Flavour) {
            sb.append(flavour);
            sb.append("&");
        }
        sb.trimToSize();
        pageContext.setAttribute("sb", sb);
    }
    out.print("this is string" + sb);
%>

CODE FOR HYPERLINK 超级链接代码

<a href="Filter_Products?${sb}page=${currentPage + 1}" style="color: white;text-decoration: none;">Next</a></td>

You do need to add sb to the pageContext, request, session or context scope to be able to access it through an EL expression - it will not pick up variables declared only in scriptlets. 您确实需要将sb添加到pageContext,请求,会话或上下文范围中,以便能够通过EL表达式访问它-它不会选择仅在scriptlet中声明的变量。 Modify as follows so that that sb is always put into the pageContext: 进行如下修改,以便始终将sb放入pageContext中:

if (request.getAttribute("Flavour") != null) {
        String[] flavours = (String[]) request.getAttribute("Flavour");
        for (String flavour : flavours) {
            sb.append(flavour);
            sb.append("&");
        }
        sb.trimToSize();

    }
    pageContext.setAttribute("sb", sb);

I have also suggested a change to improve the clarity of your code when assigning the array of 'flavour' String values. 我还建议进行更改,以在分配“风味” String值数组时提高代码的清晰度。

You are setting the value to pageContext and trying to accessed via ${}. 您正在将值设置为pageContext并尝试通过$ {}访问。 This by default refer to request scope. 默认情况下,这是指请求范围。 Try ${pageScope.sb} 试试$ {pageScope.sb}

Apart from that Your code looks fine, but i'm confused with the &&. 除此之外,您的代码看起来还不错,但是我对&&感到困惑。 Usually the URL parameter are separated by just one & symbol. 通常,URL参数仅由一个&符号分隔。 Why do you need 2? 为什么需要2?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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