首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

struts2 从action中读的内容在jsp中展示不出来

2012-09-10 
struts2 从action中读的内容在jsp中显示不出来struts.xml:XML code?xml version1.0 encodingUTF-8

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试试
------解决方案--------------------


探讨

引用:

Action里面写
public class NewsAction extends ActionSupport试试


终于发现哪有问题了。。原来是之前的get,set是要小写的。。
比如id属性,我写的是SetId(int id)
改成 setId(int id)即可。。
呵,之前一直不知道还有这个说道。。。

[解决办法]
用session试试,估计是请求转发与重定向的问题
<result name="n" type="默认是什么来着?">/index.jsp</result>
......

热点排行