简体   繁体   English

如何在jsp的forEach循环中添加会话属性?

[英]How to add a session attribute in a forEach loop in jsp?

I'm a beginner in java and need some help.我是 Java 初学者,需要一些帮助。 I'm working on a web application that allows users to manage their text projects.我正在开发一个允许用户管理其文本项目的 Web 应用程序。 I'm using Spring as a framewok and have to work with MVC model.我使用 Spring 作为框架并且必须使用 MVC 模型。

A user can have several projects.一个用户可以有多个项目。 They all are displayed in the view "My projects".它们都显示在“我的项目”视图中。 The list of all projects is managed in the foreach loop.所有项目的列表都在 foreach 循环中管理。

I want to make these project items clickable: when the user clicks on the project, it must be opened in a new page.我想让这些项目项可点击:当用户点击项目时,它必须在新页面中打开。 For that, I have to pass information about what project was klicked.为此,我必须传递有关被点击的项目的信息。 I'm trying to attach project name to session from the jsp, but the onlick part is not accepted by compiler.我正在尝试将项目名称从 jsp 附加到会话,但编译器不接受 onlick 部分。 My code abstract of this part looks as follows:我这部分的代码摘要如下:

<c:forEach var="project" items="${showProject}">
    <div class="manageListItem" onclick="window.location.assign('/myProjects.secu', 'currentProject.secu')";"<%session.setAttribute("project", ${project.title});>
    <td><c:out value="${project.title}"/></td>
</c:forEach>

Has anyone an idea how I can resolve this problem?有谁知道我如何解决这个问题? I'd be thankful for any help.我会很感激任何帮助。

Best regards, Mascha最好的问候,玛莎

Preparation准备

  • showProject is in request/session/application scope so that your jsp can access using EL. showProject在请求/会话/应用程序范围内,以便您的 jsp 可以使用 EL 访问。
  • showProject is of a Collection type showProject属于Collection类型

MVC pattern MVC模式

putting java snippet in jsp view is generally a bad idea .将 java 代码段放在 jsp 视图中通常是一个坏主意 MVC pattern should be used in your case.你的情况应该使用MVC模式。 You will need to create a servlet to get a list of project, in your servlet:您需要在 servlet 中创建一个 servlet 以获取项目列表:

// import omitted
@WebServlet("/getProjects")
public class ProjectServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // service layer that can get a list of projects
        ProjectService projectService = new ProjectService();
        List<Project> projectList = projectService.getListOfProjects();
        request.setAttribute("showProject", projectList);
        request.getRequestDispatcher("/projects.jsp").forward(request,response);
    }
}

In your projects.jsp在你的projects.jsp

<c:forEach var="project" items="${showProject}">
    <div>
    <a href="/your/project/singleProjectServlet?pid=${project.id}">${project.title}</a>
    </div>
</c:forEach>

Explanation解释

  • User clicks on a link that goes to your ProjectServlet用户单击转到您的ProjectServlet的链接
  • Your servlet gets a list of Projects (from any datasource, most likely database)您的 servlet 获取项目列表(来自任何数据源,很可能是数据库)
  • your servlet then save the list of Projects in request scope您的 servlet 然后将项目列表保存在请求范围内
  • forward to the jsp view转到jsp视图
  • your jsp view will use EL to get the list of projects form the request scope您的 jsp 视图将使用 EL 从请求范围中获取项目列表
  • every project title in the forloop is a link to a singleProjectServlet forloop 中的每个项目标题都是指向 singleProjectServlet 的链接
  • in the singleProjectServet, you will get the pid query parameter and do whatever you want such as retrieve the project details or save it in session.在 singleProjectServet 中,您将获得pid查询参数并执行您想做的任何操作,例如检索项目详细信息或将其保存在会话中。
    Here is how you can save an object in session in a Servlet:以下是在 Servlet 中的会话中保存对象的方法:

    request.getSession().setAttribute("project", projectObject); request.getSession().setAttribute("project", projectObject);

  • You get the projectObject by the pid passed in the query parameter您通过查询参数中传递的pid获取projectObject

Start from here从这里开始

It is very difficult to put everything a beginner might need to do this in this small answer area.将初学者可能需要做的所有事情都放在这个小的答案区域中是非常困难的。 Hopefully my answer will lead you to the right direction.希望我的回答能引导你走向正确的方向。 This book is highly recommended before you do anything else:在你做任何其他事情之前,强烈推荐这本书:
Murach's Java Servlets and JSP, 3rd Edition (Murach: Training & Reference) Murach 的 Java Servlets 和 JSP,第 3 版(Murach:培训和参考)

There are also plenty of simple tutorials online that can help you get started, simply search the keyword "jsp servlet mvc" .网上也有很多简单的教程可以帮助你入门,搜索关键词“jsp servlet mvc”即可
Happy coding and hope it helps.快乐编码,希望它有所帮助。

You have a wrong number of double quotes.您的双引号数量错误。

Why don't you simplify your expression and add things one after one ?你为什么不简化你的表达并一个接一个地添加东西?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM