日期:2014-05-19  浏览次数:20736 次

struts2 从action中读的内容在jsp中显示不出来
struts.xml:
XML code

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
        <package name="Type" extends="struts-default">
        <!-- 得到指定类型的所有新闻 -->
        <action name="GetNewsList" class="Action.NewsAction" method="GetNewsList">
            <result name="success">/index.jsp</result>
        </action>
        </package>
</struts>    




action:
Java code

package Action;

import java.util.ArrayList;
import java.util.Map;
import Service.NewsService;
import Vo.News;


import com.opensymphony.xwork2.ActionContext;

public class NewsAction {
    private ArrayList<News> list = new ArrayList<News>();
    private int typeId;
    public ArrayList<News> getNewsList(){
        return this.list;
    }
    
    public void setNewsList(ArrayList<News> temp){
        this.list = temp;
    }
    
    public void setTypeId(int typeId){
        this.typeId = typeId;
    }
    
    public int getTypeId(){
        return this.typeId;
    }
    
    public String GetNewsList(){
        NewsService service = new NewsService();
        //System.out.println("类型号"+getTypeId());
        list = service.GetNewsList(getTypeId());
        //System.out.println(list.size());          // 打印出来可以看到list中已经有内容了
        Map request = (Map) ActionContext.getContext().get("request");
        request.put("newsList", list);
        return "success";
    }
}




index.jsp
Java code

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<html>
<head></head>
<body>
<s:action name="GetNewsList" >
<s:param name="typeId">1</s:param>
</s:action>
<s:iterator value="#request.newsList" id="list">
    <li>
        <em><s:property value="#list.date"/></em>
        <a href="ShowNews.jsp?id=<s:property value='#list.id'/>" target="_blank"><s:property value="#list.title"/></a>
    </li>    
</s:iterator>

</body>
</html>
麻烦大家指点下,我之前用类似的方法显示另一个list正常啊。不知道为什么这次怎么就显示不出来了。谢谢大家了






------解决方案--------------------
<s:iterator value="list" id="list">
<li>
<em><s:property value="date"/></em>
<a href="ShowNews.jsp?id=<s:property value='id'/>" target="_blank"><s:property value="title"/></a>
</li>
</s:iterator>

试下
------解决方案--------------------
首先先用el表达式看看有没有值,在页面的body里面${newsList},有值的话就好办。
页面编码最好用utf-8,免得乱码
------解决方案--------------------
1.要就是你没数据.
2.你没读到数据.
3.你在页面显示的时候,也就是jsp上面写错了
------解决方案--------------------
Action里面写
public class NewsAction extends ActionSupport试试
------解决方案--------------------