0

I'm developing an ecommerce store using spring mvc. Suddenly I need to user some information in the jsp file (view) from controller class. In controller class I create a ModelAndView object and use addAttribute to use objext in my jsp file. Then I got stuck. I can not use the object in jsp file.

Hi this is my user class //package com.ecommerce.demo

package com.ecommerce.demo;

import javax.persistence.*;

@Entity
@Table(name="USER")
public class User {
    @Id 
    private String userName;
    private String userPassword;

    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserPassword() {
        return userPassword;
    }
    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }


}

and this is my controller class

package com.ecommerce.controller;

import java.io.IOException;
import java.util.List;
import javax.websocket.Session;

import org.hibernate.*;
import org.hibernate.cfg.*;
import org.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ecommerce.demo.User;

@Controller
@RequestMapping(value="mainPageController")
public class MainPageController {

    @RequestMapping(value="/mainPage.html")
    public ModelAndView getLogInForm(@ModelAttribute("user") User userInput, BindingResult result) throws IOException {

        List<User> userDatabase = null;

        userDatabase = session.createQuery("from User").list();

        session.beginTransaction();         
        session.close();

        ModelAndView model = new ModelAndView("MainPage");
        model.addObject("userNameAndPasswordList", userDatabase);       

        return model;
    }

}

Now how can I print userName and userPassword in my mainPage.jsp file?

<%@ page import="com.ecommerce.demo.User" %>

<%
    Usinf java code(i.e for loop)
    I need to print all userName and userPassword of user class here. 
%>

Tom11
  • 2,419
  • 8
  • 30
  • 56

3 Answers3

0

You should write your mainPage.jsp like this and avoid the java code in it:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
    <c:forEach items="${userNameAndPasswordList}" var="user">
        <tr>
            <td>${user.userName}</td>
            <td>${user.userPassword}</td>
        </tr>
    </c:forEach>
</table>
Blank
  • 12,308
  • 1
  • 14
  • 32
0

Use below code to fetch and print the data.

import JSTL tag in jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<table>
    <c:if test="${userNameAndPasswordList!= null}">
                    <c:forEach items="${userNameAndPasswordList}" var="user">
                        <tr>
                            <td><c:out value="${user.userName}" /></td>
                            <td><c:out value="${user.userPassword}" /></td> 
                        </tr>
                    </c:forEach>
                </c:if>
</table>
Sai prateek
  • 11,842
  • 9
  • 51
  • 66
0

Use JSTL to print the result in your JSP file.

In this case you need to use foreach tag in JSTL

Here, your result is in object userNameAndPasswordList

Using $ syntax you can access the data of your object.

        <c:forEach items="${userNameAndPasswordList}" var="userdetails">

                <c:out value="${userdetails.userName}" />
                <c:out value="${userdetails.userPassword}"/>

        </c:forEach>
Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24