Struts2有些情况下需要在jsp中嵌套调用action执行的结果

Struts2有些情况下需要在jsp中嵌套调用action执行的结果

 

1.NewsAction类

package com.mingda.action;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.mingda.entity.News;
import com.mingda.service.NewsService;

@ParentPackage("struts-default")
@Namespace("/")
public class NewsAction {
    private NewsService newsService;
    private List<News> newses;
    public List<News> getNewses() {
        return newses;
    }

    public void setNewses(List<News> newses) {
        this.newses = newses;
    }

    public NewsService getNewsService() {
        return newsService;
    }

    @Autowired
    public void setNewsService(NewsService newsService) {
        this.newsService = newsService;
    }

    @Action(value = "newsSave", results = { @Result(name = "success", location = "/news.jsp") })
    public String save() {
        News news = new News();
        news.setId(UUID.randomUUID().toString());
        news.setTitle("我爱你");
        news.setContent("我稻草打飞机卡拉电视剧考虑ffffffff!");
        news.setCreateTime(new Date());
        newsService.save(news);
        return "success";
    }

    @Action(value = "newsList", results = { @Result(name = "success", location = "/news.jsp") })
    public String list() {
        List<News> newses=newsService.listNews();
        setNewses(newses);
        return "success";
    }

}

2.struts2中action跳转页面(news.jsp)

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<c:forEach  items="${newses}" var="news">
    <ul style="width:410px;" class="list fl">
           <li><span class="fr"><fmt:formatDate value="${news.createTime}" type="date" pattern="yyyy-MM-dd"/></span><a href="news/252825.shtml"><c:out value="${news.title}"/></a></li>
       </ul>
</c:forEach>

3.jsp使用struts2的action标签调用action的页面(test.jsp)

 

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
新闻测试
<br />
<s:action name="newsList" namespace="/" executeResult="true"/>

4.常见错误:

 

[org.apache.struts2.components.ActionComponent]Could not execute action: /newsList

There is no Action mapped for action name newsList. - [unknown location]

 

问题原因:Namespace没有指定。

 

5.浏览器最后正常显示结果:

6.加入jstl c:if 标签(注意且与或的区别)

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<div class="con clearself">
    <c:forEach  items="${newses}" var="news" varStatus="index">
        <c:if test="${index.index==0}">
                <ul style="width:410px;" class="list fl">
            </c:if>
        <c:if test="${(index.index%3==0) and (index.index!=0)}">
            <ul style="width:410px; margin-left:50px; padding-left:50px; border-left:1px solid #e6e5e5;" class="list fl">
        </c:if>
               <li><span class="fr"><fmt:formatDate value="${news.createTime}" type="date" pattern="yyyy-MM-dd"/></span><a href="${news.href}"><c:out value="${news.title}"/></a></li>
        <c:if test="${index.index%3==2||(fn:length(newses)-1==index.index)}">
               </ul>
           </c:if>
    </c:forEach>
</div>

7.浏览器效果预览:

posted @ 2012-11-21 12:28  tyest  阅读(2229)  评论(0编辑  收藏  举报