【问题标题】:Spring using ModelAndView addObjectSpring使用ModelAndView addObject
【发布时间】:2014-02-11 01:21:29
【问题描述】:

我正在尝试在 jsp 页面中显示使用 addObject() 加载并通过控制器返回的对象。我没有看到 jsp 中的对象。这是我的控制器:

import java.util.ArrayList;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.apress.pss.terrormovies.model.Movie;
import com.apress.pss.terrormovies.service.MoviesService;

@Controller
@RequestMapping("/movies")
public class MovieController {

    @Autowired
    private MoviesService moviesService;

    ... Other Mapped mehtods not shown ...

    // Respond to http://localhost:8080/movies and require login
    // Get a list of movie objects in an ArrayList and return to view
    @RequestMapping(method = RequestMethod.GET, value="/")
    public ModelAndView getAllMovies() {
        ModelAndView mv = new ModelAndView("allMovies");

        // Debug
        for (Movie movie: moviesService.getAllMovies()) {
           System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX " + movie.getName());
        }

        mv.addObject("movies", moviesService.getAllMovies());
        return mv;

    }

}   

这是我实现 moviesService.getAllMoivies() 的 MoviesServiceImpl

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreFilter;
import org.springframework.security.access.prepost.PostFilter;

import com.apress.pss.terrormovies.model.Movie;

public class MoviesServiceImpl implements MoviesService {

private static final Map<String, Movie> MOVIES = new HashMap<String, Movie>();

    static {
    //System.out.println("----------------- Entering Die Hard");
    MOVIES.put("die hard", new Movie("Die Hard", "20000000"));
    // Create another movie for testing PostAuthorize in MoviesController
    //System.out.println("----------------- Entering two days in paris");
    MOVIES.put("two days in paris", new Movie("two days in paris", "1000000"));
    }

    ... Other methods not shown....

    // Allow ROLE_ADMIN to have access to movies with budget over 5000000. Other 
    // users will see only movies with budgets < 5000000
    @PreAuthorize("isFullyAuthenticated()")
    @PostFilter("hasRole('ROLE_ADMIN') or (hasRole('ROLE_USER') and T(java.lang.Integer).parseInt(filterObject.budget) < 5000000)")
    public Collection<Movie> getAllMovies() {
        return new ArrayList<Movie>(MOVIES.values());
    }
}

这是我用来显示结果的jsp页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="security"
uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Terror movies</title>
</head>
<p>Movies:</p>
<body>

<c:if test="${not empty movies}">
    <c:forEach items="${movies}" var="movie">   
        ${movie.name}<br />
    </c:forEach> 
</c:if>

</body>
</html>

最后,这是我的电影课:

public class Movie {

    private String name;
    private String budget;

    public Movie(String name, String budget) {

        super();
        this.name = name;
        this.budget = budget;
    }

    public String getName() {
    return name;
    }

    public String getBudget() {
        return budget;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setBudget(String budget) {
        this.budget = budget;
    }

    public String toString() {
        return "Title: " + name + "; Budget: " + budget;
    }

}

当我到达 URL /movies(在本地主机上)时,系统会提示我登录。当我以管理员身份登录时,我应该会看到两部电影都添加到 MoviesServiceImpl 中的 MOVIES 地图中。我可以看到静态块中的调试加载电影。我可以在 MovieController.getAllMovies() 方法中看到调试访问的电影。我被正确定向到 allMovies.jsp 页面,但唯一的输出是“电影:”。如果我删除 allMovies.jsp 中 for 循环周围的检查,我会得到以下输出:电影:${movie.name}。没有抛出异常或我可以看到的其他错误,但是我不相信我正确使用了 ModelAndView.addObject() 。一些帮助将不胜感激。提前谢谢你。

更新: 如果我在我的 jsp 页面中加入以下语句: 我将得到以下输出:“jsp: movie [Title: Die Hard; Budget: 20000000, Title: two days in paris; Budget: 1000000]” 所以对象数组正在进入 jsp 页面,我只是没有正确访问它但看不到错误。

【问题讨论】:

  • 如果您在删除检查时在 jsp 中看到 ${movie.name},那么似乎没有对 JSTL 表达式进行评估。你能发布你的web.xml吗?

标签: spring modelandview


【解决方案1】:

您能否使用 Model.addAttribute(name, value) 而不是 ModelAndView.addObject(name, value) 进行检查? 我认为您也应该使用 Model.addAttribute 方法遇到同样的问题。

请尝试添加以下内容

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

到 JSP。

【讨论】:

  • 我尝试使用 Model.addAttributes 并遇到了同样的问题。我在jsp中添加了 springframework.org/tags"%> 仍然遇到同样的问题。
【解决方案2】:

对于那些可能有类似问题的人,答案竟然是我的 web.xml 文件。这个例子来自 Pro Spring Security 一书。作者以前面的例子为基础来说明概念。在这个例子中,作者没有提到从使用 DTD 而不是 XML Schema 的早期版本更新 web.xml 文件。我不得不将我的 web.xml 更改为:

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
...
</web-app>

收件人:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 
...
</web-app>

EL 表达式没有被计算。现在工作正常。

【讨论】:

    【解决方案3】:

    通过修改解决

    <web-app> 
    

    作为

    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      version="3.0"
      metadata-complete="true">
    

    【讨论】:

      猜你喜欢
      • 2015-04-27
      • 2020-05-24
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多