【问题标题】:Spring MVC model doesn't know my classSpring MVC 模型不知道我的类
【发布时间】:2014-12-19 15:59:10
【问题描述】:

我有一个应该直截了当的问题,我显然做错了什么。 我有一个用 Spring MVC 编写的简单站点。我的 JSP 模型不知道我的 NewItem 类..

@RequestMapping(value = "/", method = RequestMethod.GET)
public String newsFeed(Model model) {       
    try {
        initDB();
    } catch (Exception e) {     
        e.printStackTrace();
    }   

    ScanRequest scanRequest = new ScanRequest()
    .withTableName("Table1");
    ScanResult result = _database.get(scanRequest);         
    //This will just return a list with filled NewsItems from the database  
    List<NewsItem> list = getNewsItems(result.getItems());      
    model.addAttribute("newsList",list);        
    return "newsfeed";
}

public class NewsItem  {    
    private String url="";
    String getUrl(){
        return url;
    }
    void setUrl(String text) {
        url = text;
    }
    private String title="";
    void setTitle(String text) {
        title = text;
        Image = text;
    }

    String getTitle(){
        return title;
    }   
    String Description="";    
    String Image="";
    String Time="";
    String Since="";
}

当我在 JSP 端使用 ${newsList.getTile()} 时,我得到了一个 javax.servlet.jsp.JspException:javax.el.MethodNotFoundException: 找不到方法:类 java.util.ArrayList.getTile() 或 java.lang.NumberFormatException:对于输入字符串:.. 当我试图 使用 ${newsList.Image}。有谁知道我在这里缺少什么?

我在 jsp 中使用的东西。 taglib uri="http://tiles.apache.org/tags-tiles" 前缀=“瓷砖”标签库前缀=“弹簧” uri="http://www.springframework.org/tags" 标签库 uri="http://java.sun.com/jsp/jstl/core" 前缀="c" 页面 import="com.kiiak.tennman.NewsItem"

【问题讨论】:

    标签: java spring model-view-controller model


    【解决方案1】:

    NewsItem 而不是List 本身上调用getTitle(注意拼写)

    <c:forEach var="newsItem" items="${newsList}">
        ${newsItem.title}
    </c:forEach>
    

    【讨论】:

    • 这就是我认为的答案。
    • 嗨,谢谢你的回答。现在我得到了相同的异常,但异常指向正确的类而不是 ArrayList.. 找不到属性“标题”。如果我尝试 getTitle() 在类 com.x.y.NewsItem. 中找不到方法,我是否需要注册该类或固有的某种接口才能使其工作?
    • getTitle(和所有其他 bean 方法)应该是 public
    猜你喜欢
    • 1970-01-01
    • 2015-08-21
    • 2011-02-18
    • 2017-08-24
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    相关资源
    最近更新 更多