简体   繁体   English

如何在JSP中将数组打印到输入字段?

[英]How to print an array to an input field in jsp?

An array of integers fibSequence is passed over to a jsp page result using a redirect which is retrieved like this: <%String[] fibSequence = request.getParameterValues("fibSequence");%> . 整数数组fibSequence通过使用如下所示的重定向传递到jsp页面result<%String[] fibSequence = request.getParameterValues("fibSequence");%> But when I set the input field's value to the fibSequence array I get a memory address of the array and not the integer values stored in the array: 但是,当我将输入字段的值设置为fibSequence数组时,我得到的是该数组的内存地址,而不是该数组中存储的整数值:

[Ljava.lang.String;@678f482d

内存地址

This is how the array is being output to the text box: 这是数组输出到文本框的方式:

<input type="text" name="fibNum" value="<%=fibSequence%>" size="40px" style="font-size:30pt;height:60px">

and also I've tried like this from the below answer but the output is still the same: 而且我已经从下面的答案中尝试过这样,但是输出仍然是相同的:

<input type="text" name="fibNum" value="<%=java.util.Arrays.deepToString(fibSequence)%>" size="40px" style="font-size:30pt;height:60px">

Does anyone know how the contents of the array can be output to a textbox in jsp? 有谁知道如何将数组的内容输出到jsp中的文本框?

I have tried to use the Arrays.toString method to print out the values, but I get an error Arrays cannot be resolved: 我尝试使用Arrays.toString方法打印出值,但出现错误Arrays无法解决:

<%=Arrays.toString(fibSequence)%>

You're getting the default Object.toString() because array (and array is an Object) doesn't override toString() . 您将获得默认的Object.toString()因为array(并且array是一个Object)不会覆盖toString() You could use Arrays.toString(Object[]) like 你可以像这样使用Arrays.toString(Object[])

value="<%=java.util.Arrays.toString(fibSequence)%>"

Or add the import for java.util.Arrays to your JSP. 或将java.util.Arrays的导入添加到您的JSP中。

This example is work: 这个例子是可行的:
web.xml web.xml

<web-app>
  <display-name>Archetype Created Web Application</display-name>
    <filter>
        <filter-name>filter</filter-name>
        <filter-class>ru.bmstu.FirstFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>filter</filter-name>
        <url-pattern>*</url-pattern>
    </filter-mapping>
</web-app>

FirsFilter.java FirsFilter.java

...
    void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("doFilter from FirstFilter");
        String[] cba = {"1", "2", "3", "5"};
        request.setAttribute("cba", cba);
        filterChain.doFilter(request, response);
    }
...

index.jsp index.jsp

<%@ page import="java.util.Arrays" %>
<html>
<body>
<h3>This is the JBoss example!</h3>
<% String[] abc = {"1", "2", "3"};%>
<%=Arrays.toString(abc)%>
<% String[] cba = (String[]) request.getAttribute("cba"); %>
<%=Arrays.toString(cba)%>
</body>
</html>

Result is: 结果是:

This is the JBoss example!
[1, 2, 3] [1, 2, 3, 5]

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

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