Mascha
Mascha

Reputation: 21

How to add a session attribute in a forEach loop in jsp?

I'm a beginner in java and need some help. I'm working on a web application that allows users to manage their text projects. I'm using Spring as a framewok and have to work with MVC model.

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.

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. 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

Upvotes: 1

Views: 2769

Answers (2)

Alex Minjun Yu
Alex Minjun Yu

Reputation: 3707

Preparation

  • showProject is in request/session/application scope so that your jsp can access using EL.
  • showProject is of a Collection type

MVC pattern

putting java snippet in jsp view is generally a bad idea. MVC pattern should be used in your case. You will need to create a servlet to get a list of project, in your 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

<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
  • Your servlet gets a list of Projects (from any datasource, most likely database)
  • your servlet then save the list of Projects in request scope
  • forward to the jsp view
  • your jsp view will use EL to get the list of projects form the request scope
  • every project title in the forloop is a link to a 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.
    Here is how you can save an object in session in a Servlet:

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

  • You get the projectObject by the pid passed in the query parameter

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)

There are also plenty of simple tutorials online that can help you get started, simply search the keyword "jsp servlet mvc".
Happy coding and hope it helps.

Upvotes: 1

mvera
mvera

Reputation: 946

You have a wrong number of double quotes.

Why don't you simplify your expression and add things one after one ?

Upvotes: 0

Related Questions