Java - 将字符串转换为列表
这是将字符串转换为列表的正确方法吗?
List styles = (List)request.getParameter("styles");
Model (BeerExpert.java)
package com.example.model;
import java.util.*;
public class BeerExpert {
public List getBrands(String color){
List brands = new ArrayList();
if(color.equals("amber")){
brands.add("Jack Amber");
brands.add("Red Moose");
}
else{
brands.add("Jail Pale Ale");
brands.add("Gout Scott");
}
return brands;
}
}
接下来是 servlet 类,
BeerSelect.java
package com.example.web;
import com.example.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Beer Selection Advice <br>");
String c = request.getParameter("color");
BeerExpert be = new BeerExpert();
List result = be.getBrands(c);
request.setAttribute("styles", result);
RequestDispatcher view = request.getRequestDispatcher("results.jsp");
view.forward(request, response);
}
}
最后是 jsp。
results.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1 align="center">Beer Recommendations in JSP!!!</h1>
<%
List styles = (List)request.getParameter("styles");
Iterator it = styles.iterator();
while(it.hasNext()){
out.print("<br> try " + it.hasNext());
}
%>
</body>
</html>
谢谢
Is this the right way to convert a string to a list?
List styles = (List)request.getParameter("styles");
Model (BeerExpert.java)
package com.example.model;
import java.util.*;
public class BeerExpert {
public List getBrands(String color){
List brands = new ArrayList();
if(color.equals("amber")){
brands.add("Jack Amber");
brands.add("Red Moose");
}
else{
brands.add("Jail Pale Ale");
brands.add("Gout Scott");
}
return brands;
}
}
The next is the servlet class
BeerSelect.java
package com.example.web;
import com.example.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Beer Selection Advice <br>");
String c = request.getParameter("color");
BeerExpert be = new BeerExpert();
List result = be.getBrands(c);
request.setAttribute("styles", result);
RequestDispatcher view = request.getRequestDispatcher("results.jsp");
view.forward(request, response);
}
}
Finally the jsp.
results.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1 align="center">Beer Recommendations in JSP!!!</h1>
<%
List styles = (List)request.getParameter("styles");
Iterator it = styles.iterator();
while(it.hasNext()){
out.print("<br> try " + it.hasNext());
}
%>
</body>
</html>
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请记住,您将无法更改列表的内容。如果您需要这样做,请手动从此列表创建另一个列表。
Just keep in mind that you won't be able to change the contents of the list. If you need to do that, create another list from this one manually.
通过您提供的附加 servlet/JSP 上下文,代码中真正的错误似乎是在 JSP 页面中使用了
request.getParameter
:该方法确实返回了一个String
,并且您无法转换List
中的String
,不能使用强制转换,甚至不能使用语言或数据结构允许的任何其他操作。您可以使用已建议的方法之一将字符串
插入到列表
中(或者转换>List
到String
使用其他方法),但从代码来看这不是您需要的。在 servlet 代码中,您将
styles
属性 设置为包含啤酒品牌的List
。因此,要取回List
,您需要调用request.getAttribute
而不是getParameter
。getAttribute
方法返回一个Object
,它实际上是一个List
,并且您知道,因为您已将其设置为这样,所以在在这种情况下,强制转换正是获取原始类型值所需的操作。在代码中,这意味着在您的 JSP 中,代替给您带来麻烦的行。
With the additional servlet/JSP context you provided, it seems that the real mistake in your code is the use of
request.getParameter
in the JSP page: that method indeed returns aString
, and you can't convert aString
in aList
, not with a cast, not even with any other operation allowed by the language or the data structures. You may insert aString
into aList
, using one of the methods already suggested (or transform aList
into aString
using other methods), but judging from the code that's not what you need.In the servlet code, you set the
styles
attribute to theList
containing the beer brands. So, to get thatList
back, you need to invokerequest.getAttribute
instead ofgetParameter
. ThegetAttribute
methods returns anObject
, which really is aList
, and you know that because you have set it to be as such, so in this case a cast is exactly the operation that is needed to get back the value with its original type. In code, this meansin your JSP, in place of the line that got you troubles.
事实并非如此,您正在做的是尝试将
String
转换为List
,这是不一样的。 强制转换不会转换对象,它只是试图告诉对象是什么类型。你需要做的是:
It's not, what you're doing is trying to cast a
String
to aList
, which is not the same. A cast doesn't convert object, it merely tries to tell what type an object is.What you need to do is:
如果您尝试检索更多“样式”,您可以使用:
它将返回一个字符串数组,该数组可用于创建列表,如下所示:
If you are trying to retrieve more "styles" you can use:
that will return an array of String, that could be used to create a List as follows: