开发者

Question about Struts2 return type

开发者 https://www.devze.com 2023-03-05 16:32 出处:网络
I have the following scenario: Struts.xml <action name=\"user_initNews\" method=\"initNews\" class=\"it.sba.bcc.sbarima.user.web.action.UserAction\">

I have the following scenario:

Struts.xml

    <action name="user_initNews" method="initNews" class="it.sba.bcc.sbarima.user.web.action.UserAction">
        <result type="dispatcher">
            <param name="location">pages/elementicomuni/elencoNews.jsp</param>
        </result>
    </action>

User Action

public class UserAction extends BaseAction
{

    private NewsService newsService = null;

    private User utente;
    private List<News> news;

    public String initNews()
    {
            return SUCCESS;
    }

    public void elencoNews()
    {
        try
        {
            newsService = UserServiceFactory.getNewsService();
            this.news = newsService.getNews(ge开发者_JAVA技巧tAbiUserProfile(), getMatricolaUserProfile());
        } 
        catch (ServiceException e)
        {
            e.printStackTrace();
        }
    }

    public List<News> getNews()
    {
        return news;
    }
}

elencoNews.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?xml version="1.0" encoding="utf-8"?>

<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
    <body>
        <s:action name="user_elencoNews!elencoNews"></s:action>
        <s:iterator value="news" var="n">
            <label><s:property value="descrizione"/></label>
        </s:iterator>
    </body>
</html>

Whene the elencoNews.jsp is rendered, I would like to call from the JSP page an action thath return a set of POJO to iterate.

The elencoNews.action is correctly called, but I do not know how to treat data on the elencoNews.jap

How can I do that?


I'm afraid you are misunderstanding the whole Struts2 typical workflow. You dont (typically) want to call another action from the JSP, in the typical workflow the JSP renders after your action has processed your request, and it just displays data (typically from your Action instance, which is found in the value stack). Eg read here

In your example, you'd should code in your JPS:

<s:iterator value="news" var="n">
    <s:property value="descrizione"/>
</s:iterator>

If you have this concepts clear (and you have coded the most basic-typical struts2 cases), the disregard this and be more explicit about what are you trying to accomplish - why you have two actions involved for a single request and jsp.


If you want a dynamic jsp page, you can look into using the Struts2-jQuery plugin which will allow you to call another action in your JSP and load the results. See examples at http://www.weinfreund.de/struts2-jquery-showcase/index.action under Ajax Link or Ajax Div sections.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号