Java 将特定元素从servlet模型中的arraylist传递给jsp
所以更具体地说,我想解决什么问题。如何在jsp中显示Arraylist中的第一个元素:“sortedDomainList” 编辑:在普通java应用程序中尝试使用模型和ArrayList 型号短版Java 将特定元素从servlet模型中的arraylist传递给jsp,java,jsp,servlets,arraylist,Java,Jsp,Servlets,Arraylist,所以更具体地说,我想解决什么问题。如何在jsp中显示Arraylist中的第一个元素:“sortedDomainList” 编辑:在普通java应用程序中尝试使用模型和ArrayList 型号短版 public ArrayList<String> sortedDomainList = new ArrayList<String>(); public ArrayList<String> getSortedDomainList() {
public ArrayList<String> sortedDomainList = new ArrayList<String>();
public ArrayList<String> getSortedDomainList() {
return sortedDomainList;
}
public void setSortedDomainList(ArrayList<String> sortedDomainList) {
this.sortedDomainList = sortedDomainList;
}
}
public ArrayList sortedDomainList=new ArrayList();
公共阵列列表getSortedDomainList(){
返回主列表;
}
公共无效setSortedDomainList(ArrayList sortedDomainList){
this.sortedDomainList=sortedDomainList;
}
}
控制器/SERVLET
package com.comparebet.controller;
import com.comparebet.model.*;
@WebServlet("/Controller")
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
public Controller() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
ArrayList<String> sortedDomainList = new BeanModel().getSortedDomainList();
request.setAttribute("d", sortedDomainList);
RequestDispatcher view = request.getRequestDispatcher("view.jsp");
view.forward(request, response);
}
}
package com.comparebet.controller;
导入com.comparebet.model.*;
@WebServlet(“/Controller”)
公共类控制器扩展HttpServlet{
私有静态最终长serialVersionUID=1L;
公共控制员(){
超级();
}
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
}
public void doPost(HttpServletRequest请求、HttpServletResponse响应)引发IOException、ServletException{
ArrayList sortedDomainList=新BeanModel().getSortedDomainList();
request.setAttribute(“d”,主列表);
RequestDispatcher视图=request.getRequestDispatcher(“view.jsp”);
视图。转发(请求、响应);
}
}
查看/JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>CompareBet</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/Comparebet" method="get" accept-charset="UTF-8"></form>
<h1> ${d.sortedDomainList[0]}</h1>
</body>
</html>
比目鱼
${d.mainlist[0]}
这是我第一次尝试MVC,所以当涉及到servlet时,我感到非常失落。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>CompareBet</title>
</head>
<body>
<%
ArrayList<String> sortedDomainList= request.getAttribute("d");
for(String domain:sortedDomainList){
%>
<h1> <%= domain %> </h1>
<% } %>
</body>
</html>
比目鱼
现在,当我将其作为应用程序运行时,我测试了它的工作情况,因此这是一个jsp servlet问题。编辑完成。好的,我明天就试试,现在累了。刚刚花了几个小时阅读了更多关于Servlet和jsp的内容,但仍然感到困惑:)有些人把代码放在jsp中,但有些人严格拒绝。当我请求sortedDomainList时,它会执行我模型中的所有代码吗?System.out.println(sortedDomainList)当我尝试此操作时,什么都不会发生。当我将代码改为:而不是/Comparebet/Comparebet时,它会起作用/*