从servlet中将特定元素从模型中的arraylist传递给jsp

时间:2016-01-29 21:37:18

标签: java jsp servlets arraylist

更具体地说,我试图解决的问题。如何在Arraylist中显示第一个元素:" sortedDomainList"在我的jsp?

编辑:模型和ArrayList在普通的java应用程序中尝试过。

模型简短版本。

    public ArrayList<String> sortedDomainList = new ArrayList<String>();



    public ArrayList<String> getSortedDomainList() {
        return sortedDomainList;
    }


    public void setSortedDomainList(ArrayList<String> 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);
        }
    }

查看/ 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>

这是我对MVC的第一次尝试,所以当涉及到servlet时我感到非常失落。

1 个答案:

答案 0 :(得分:-1)

public void AddUserToDMSite(string useremail, string securityGroupName, Web aWeb)
    {
 GroupCollection collGroup = aWeb.SiteGroups;
 Group oGroup1 = collGroup.GetByName("UserList");
 Group oGroup2 = collGroup.GetByName(securityGroupName);
 UserCollection oUserCollection1 = oGroup1.Users;
 UserCollection oUserCollection2 = oGroup2.Users;
 SPContext.Load(oUserCollection1);
 SPContext.Load(oUserCollection2);
 SPContext.ExecuteQuery();
 var uname = oGroup1.Users.GetByEmail(useremail);
 var userCheck = oUserCollection2.Where(u => u.Email == useremail).FirstOrDefault();
 if (userCheck == null)
 {
      Microsoft.SharePoint.Client.User oUser2 = oGroup2.Users.AddUser(uname);
 }
 SPContext.ExecuteQuery(); 
 }