Java 将字符串转换为整数 JSP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27219898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:59:40  来源:igfitidea点击:

Convert String to Integer JSP

javahtmljspscriptlet

提问by carteruk

I am a beginner using JSP. I want to display a list of incrementing integers using a maximum range of the users choice.

我是使用 JSP 的初学者。我想使用用户选择的最大范围显示递增整数列表。

Entering: 6 should display the following:

输入:6 应显示以下内容:

  • number 1
  • number 2
  • number 3
  • number 4
  • number 5
  • number 6
  • 1号
  • 2号
  • 3号
  • 4号
  • 5号
  • 6号

input.jsp

输入.jsp

<body>
<input type="number" name="numberMax" required>
<input type="submit" value="submit">
</body>

jspResult.jsp

jspResult.jsp

<body>
<% 
int numberMax = request.getParameter("numberMax");  // Cannot convert from String to int
%>
for (int i = 1; i <= numberMax; i++) 
{ %>
<ul>
<li><%= i %></li>
</ul>
<% } %>
</body>

How can I convert the input to an integer in order for the jsp scriptlet to print.

如何将输入转换为整数以便 jsp scriptlet 打印。

采纳答案by Megha Sharma

Try using this:

尝试使用这个:

<%int no = Integer.parseInt(request.getParameter("numberMax"));%>

<%int no = Integer.parseInt(request.getParameter("numberMax"));%>

Its working for me.

它为我工作。

回答by Jerome Anthony

Try using `Integer.parseInt()' to convert string to integer.

尝试使用 `Integer.parseInt()' 将字符串转换为整数。

<% 
 String paramNumMax = request.getParameter("numberMax");  // Cannot convert from String to int
 int numberMax = Integer.parseInt(paramNumMax.trim());
%>

回答by Joop Eggen

You can use JSTL tags. The conversion from String to int then is done in the Expression Language.

您可以使用 JSTL 标记。然后在表达式语言中完成从 String 到 int 的转换。

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
    <head>
       <meta charset="UTF-8">
    </head>
<body>
    <p>There are ${param.numberMax} numbers.</p>
    <ul>
    <c:forEach begin="1" end="${param.numberMax}" varStatus="no">
        <li><c:out value="number ${no.count}"/></li>
    </c:forEach>
    </ul>
</body>

The request parameters one gets as param.numberMaxinside ${...}. The varStatusobject contains properties like first or last time in loop and such. Here the <c:out >tag is not needed, it can escape XML entities like turning a &into correct &amp;.

一个获取的请求参数为param.numberMaxinside ${...}。该varStatus对象包含循环中的第一次或最后一次等属性。这里<c:out >不需要标记,它可以转义 XML 实体,例如将 a&转换为正确的&amp;.

回答by Abdulrehman

Best and easiest way to convert String into Integer:

将字符串转换为整数的最佳和最简单的方法:

String val="67";
int value=Integer.valueOf(val);

回答by hvsp

In case someone else lands here and is not allowed to use scripting elements for some reason. You could use

以防其他人在这里登陆并且由于某种原因不允许使用脚本元素。你可以用

<fmt:parseNumber var="intValue" value="${integerAsString}" integerOnly="true"/>

<fmt:parseNumber var="intValue" value="${integerAsString}" integerOnly="true"/>

to set a new JSP variable.

设置一个新的 JSP 变量。