Java 发送布尔值作为请求参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29263806/
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
Sending boolean as request parameter
提问by JamalU
In my web application I have a link - "Create New User". From the jsp
I am sending some request to the server like -
在我的 Web 应用程序中,我有一个链接 - “创建新用户”。从jsp
我向服务器发送一些请求,如 -
<div style="float:right" class="view">
<a href="/some/url/createUserMVC.do?hasCreatePermission=${user.hasPermission['createUser']}">Create New User</a>
</div>
Here user.hasPermission[]
is an array of boolean
. If the current user (that is user
) has the permission(that is 'createUser') to create an new user than it returns true.
这user.hasPermission[]
是一个数组boolean
。如果当前用户(即user
)具有创建新用户的权限(即“createUser”),则返回 true。
Now from my controller I am trying to get the value from the request parameter, like -
现在从我的控制器我试图从请求参数中获取值,比如 -
request.getParameter("hasCreatePermission");
But the problem is request.getParameter()
returns a String
. So how can I get the boolean value from the parameter. There is no overloaded version of request.getParameter()
method for boolean
.
但问题是request.getParameter()
返回一个String
. 那么如何从参数中获取布尔值。没有request.getParameter()
方法的重载版本boolean
。
采纳答案by silentprogrammer
I don't think it is possible. Request is always String content. But you can do
我不认为这是可能的。请求始终是字符串内容。但是你可以做
boolean hasCreatePermission= Boolean.parseBoolean(request.getParameter("hasCreatePermission"));
回答by Ria
If you are sure it's a boolean you can use
如果您确定它是一个布尔值,您可以使用
boolean value = Boolean.valueOf(yourStringValue)
回答by Ansemo Abadía
All parameters are translated by servelt as String. You need to convert String's value to Boolean.
所有参数都由 servelt 翻译为字符串。您需要将字符串的值转换为布尔值。
Boolean.parseBoolean(request.getParameter("hasCreatePermission"));
To avoid manual parsing, you have to use a framework like Spring MVC or Struts.
为了避免手动解析,您必须使用像 Spring MVC 或 Struts 这样的框架。
回答by Sathish Kumar Gurunathan
In servlet it will accept all inputs given by users as string.So, we should parse the input after we got the inputs.
在servlet中,它将接受用户提供的所有输入作为字符串。因此,我们应该在获得输入后解析输入。
e.g
例如
boolean flag = Boolean.parseBoolean(req.getParameter("Flag "));
I think this will be useful for you.
我认为这对你有用。