Reputation: 21
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
Reputation: 3707
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>
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);
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
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