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