【问题标题】:Retrive object from controller class to jsp file [duplicate]从控制器类检索对象到jsp文件[重复]
【发布时间】:2016-07-21 06:03:24
【问题描述】:

我正在使用 spring mvc 开发电子商务商店。突然我需要从控制器类中使用 jsp 文件(视图)中的一些信息。在控制器类中,我创建了一个ModelAndView 对象并使用addAttribute 在我的jsp 文件中使用objext。然后我就卡住了。我无法使用 jsp 文件中的对象。

嗨,这是我的用户类 //打包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;
    }


}

这是我的控制器类

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;
    }

}

现在如何在我的 mainPage.jsp 文件中打印 userNameuserPassword

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

【问题讨论】:

    标签: java jsp spring-mvc model-view-controller controller


    【解决方案1】:

    你应该这样写你的mainPage.jsp,并避免其中的java代码:

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

    【讨论】:

    • 我收到这样的错误:org.springframework.web.servlet.ModelAndView 无法转换为 org.springframework.ui.Model
    • 你应该在你的 spring 配置中配置视图解析器。
    • 我是新来的,你能告诉我怎么做,或者任何资源链接。
    【解决方案2】:

    使用JSTL 将结果打印到您的 JSP 文件中。

    这种情况下需要在JSTL中使用foreach标签

    这里,你的结果在对象userNameAndPasswordList

    使用 $ 语法,您可以访问对象的数据。

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

    【讨论】:

      【解决方案3】:

      使用下面的代码来获取和打印数据。

      在jsp中导入JSTL标签

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

      【讨论】:

        猜你喜欢
        • 2021-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-09
        • 1970-01-01
        相关资源
        最近更新 更多