You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by xnpeng <xn...@163.com> on 2009/04/29 12:49:57 UTC

How to forward request in struts 2.1.6?

from:

ServletActionContext.getServletContext().getRequestDispatcher("/post/message/"+id).forward(ServletActionContext.getRequest(),ServletActionContext.getResponse());

I want to forward request to http://localhost:8080/post/message/3166,

but it always throws out exception says requested resource not exist.

when replace the string with "/index.jsp",it can work correctly.

so, anyone who can help? I just want to forward request to an ACTION other than jsp file.

thanks.

Re:Re: How to forward request in struts 2.1.6?

Posted by xnpeng <xn...@163.com>.
by the idea of Convention-plugin(CodeBehind-plugin)--"Zero Config",it provides:
Action location by package naming conventions 
Result (JSP, FreeMarker, etc) location by naming conventions 
Class name to URL naming convention 
Package name to namespace convention 
SEO compliant URLs (i.e. my-action rather than MyAction) 
the normal mappings don't work in the REST-plugin environment.




在2009-05-01,"Dave Newton" <ne...@yahoo.com> 写道:
>xnpeng wrote:
>> public class PublishController extends PublishAction implements Preparable {
>>     private static String message_url = "/post/message/";
>>     private static String affair_url = "/post/affair/";
>>     private static String business_url = "/post/business/";
>>     private static String activity_url = "/post/activity/";
>>     private static String announce_url = "/post/announce/";
>>     private static String house_url = "/post/house/";
>>     private static String case_url = "/post/case/";
>>     private static String scenery_url = "/post/scenery/";
>>     private static String poll_url = "/post/poll/";
>> 
>>         if (category != null) {
>>             if (category.getCateId().equalsIgnoreCase(Category.MESSAGE)) {
>>                 ServletActionContext.getResponse()
>>                         .sendRedirect(ServletActionContext.getRequest().getContextPath() + message_url + "new");
>>             } else if (category.getCateId().equalsIgnoreCase(Category.AFFAIR)) {
>>                 ServletActionContext.getResponse()
>>                         .sendRedirect(ServletActionContext.getRequest().getContextPath() + affair_url + "new");
> > [...snip...]
>
>I'm not sure I understand the issue either, but man, isn't this what 
>maps were made for?!
>
>Dave
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>

Re: How to forward request in struts 2.1.6?

Posted by Dave Newton <ne...@yahoo.com>.
xnpeng wrote:
> public class PublishController extends PublishAction implements Preparable {
>     private static String message_url = "/post/message/";
>     private static String affair_url = "/post/affair/";
>     private static String business_url = "/post/business/";
>     private static String activity_url = "/post/activity/";
>     private static String announce_url = "/post/announce/";
>     private static String house_url = "/post/house/";
>     private static String case_url = "/post/case/";
>     private static String scenery_url = "/post/scenery/";
>     private static String poll_url = "/post/poll/";
> 
>         if (category != null) {
>             if (category.getCateId().equalsIgnoreCase(Category.MESSAGE)) {
>                 ServletActionContext.getResponse()
>                         .sendRedirect(ServletActionContext.getRequest().getContextPath() + message_url + "new");
>             } else if (category.getCateId().equalsIgnoreCase(Category.AFFAIR)) {
>                 ServletActionContext.getResponse()
>                         .sendRedirect(ServletActionContext.getRequest().getContextPath() + affair_url + "new");
 > [...snip...]

I'm not sure I understand the issue either, but man, isn't this what 
maps were made for?!

Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re:Re: Re: How to forward request in struts 2.1.6?

Posted by xnpeng <xn...@163.com>.
so many thanks.
forgive my poor english.
 
there are some words in this document:
http://struts.apache.org/2.1.6/docs/rest-plugin.html



Also, notice we aren't returning the usual "success" result code in either method. This allows us to use the special features of the Codebehind Plugin to intuitively select the result template to process when this resource is accessed with the .xhtml extension. In this case, we can provide a customized XHTML view of the resource by creating /orders-show.jsp and /orders-update.jsp for the respective methods.

my code is like this:

package com.xunan.sitexa.rest;

import com.opensymphony.xwork2.Preparable;
import com.xunan.sitexa.action.PublishAction;
import com.xunan.sitexa.post.data.Category;
import com.xunan.sitexa.post.service.CategoryService;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import javax.servlet.ServletException;
import java.io.IOException;

/**
 * User: xnpeng
 * Date: 2009-4-21
 * Time: 20:02:50
 */
public class PublishController extends PublishAction implements Preparable {
    private static String message_url = "/post/message/";
    private static String affair_url = "/post/affair/";
    private static String business_url = "/post/business/";
    private static String activity_url = "/post/activity/";
    private static String announce_url = "/post/announce/";
    private static String house_url = "/post/house/";
    private static String case_url = "/post/case/";
    private static String scenery_url = "/post/scenery/";
    private static String poll_url = "/post/poll/";

    public void prepare() {
        site = getHome();
    }

    public HttpHeaders index() throws IOException {
        String cateId = ServletActionContext.getRequest().getParameter("cateId");
        if (cateId != null && !cateId.equals(""))
            category = CategoryService.getCategory(cateId);

        if (category != null) {
            if (category.getCateId().equalsIgnoreCase(Category.MESSAGE)) {
                ServletActionContext.getResponse()
                        .sendRedirect(ServletActionContext.getRequest().getContextPath() + message_url + "new");
            } else if (category.getCateId().equalsIgnoreCase(Category.AFFAIR)) {
                ServletActionContext.getResponse()
                        .sendRedirect(ServletActionContext.getRequest().getContextPath() + affair_url + "new");
            } else if (category.getCateId().equalsIgnoreCase(Category.BUSINESS)) {
                ServletActionContext.getResponse()
                        .sendRedirect(ServletActionContext.getRequest().getContextPath() + business_url + "new");
            } else if (category.getCateId().equalsIgnoreCase(Category.ACTIVITY)) {
                ServletActionContext.getResponse()
                        .sendRedirect(ServletActionContext.getRequest().getContextPath() + activity_url + "new");
            } else if (category.getCateId().equalsIgnoreCase(Category.ANNOUNCE)) {
                ServletActionContext.getResponse()
                        .sendRedirect(ServletActionContext.getRequest().getContextPath() + announce_url + "new");
            } else if (category.getCateId().equalsIgnoreCase(Category.HOUSE)) {
                ServletActionContext.getResponse()
                        .sendRedirect(ServletActionContext.getRequest().getContextPath() + house_url + "new");
            } else if (category.getCateId().equalsIgnoreCase(Category.CASE)) {
                ServletActionContext.getResponse()
                        .sendRedirect(ServletActionContext.getRequest().getContextPath() + case_url + "new");
            } else if (category.getCateId().equalsIgnoreCase(Category.SCENERY)) {
                ServletActionContext.getResponse()
                        .sendRedirect(ServletActionContext.getRequest().getContextPath() + scenery_url + "new");
            } else if (category.getCateId().equalsIgnoreCase(Category.POLL)) {
                ServletActionContext.getResponse()
                        .sendRedirect(ServletActionContext.getRequest().getContextPath() + poll_url + "new");
            }
        }
        return new DefaultHttpHeaders("");
    }

    public HttpHeaders show() throws IOException, ServletException {

        String cateId = ServletActionContext.getRequest().getParameter("cateId");
        if (cateId != null && !cateId.equals(""))
            category = CategoryService.getCategory(cateId);

        String id = ServletActionContext.getRequest().getParameter("id");

        if (category != null && id != null) {
            if (category.getCateId().equalsIgnoreCase(Category.MESSAGE)) {
                ServletActionContext.getServletContext().getRequestDispatcher(message_url + id + "/show")
                        .forward(ServletActionContext.getRequest(), ServletActionContext.getResponse());
            } else if (category.getCateId().equalsIgnoreCase(Category.AFFAIR)) {
                ServletActionContext.getServletContext().getRequestDispatcher(affair_url + id + "/show")
                        .forward(ServletActionContext.getRequest(), ServletActionContext.getResponse());
            } else if (category.getCateId().equalsIgnoreCase(Category.BUSINESS)) {
                ServletActionContext.getServletContext().getRequestDispatcher(business_url + id + "/show")
                        .forward(ServletActionContext.getRequest(), ServletActionContext.getResponse());
            } else if (category.getCateId().equalsIgnoreCase(Category.ACTIVITY)) {
                ServletActionContext.getServletContext().getRequestDispatcher(activity_url + id + "/show")
                        .forward(ServletActionContext.getRequest(), ServletActionContext.getResponse());
            } else if (category.getCateId().equalsIgnoreCase(Category.ANNOUNCE)) {
                ServletActionContext.getServletContext().getRequestDispatcher(announce_url + id + "/show")
                        .forward(ServletActionContext.getRequest(), ServletActionContext.getResponse());
            } else if (category.getCateId().equalsIgnoreCase(Category.HOUSE)) {
                ServletActionContext.getServletContext().getRequestDispatcher(house_url + id + "/show")
                        .forward(ServletActionContext.getRequest(), ServletActionContext.getResponse());
            } else if (category.getCateId().equalsIgnoreCase(Category.CASE)) {
                ServletActionContext.getServletContext().getRequestDispatcher(case_url + id + "/show")
                        .forward(ServletActionContext.getRequest(), ServletActionContext.getResponse());
            } else if (category.getCateId().equalsIgnoreCase(Category.SCENERY)) {
                ServletActionContext.getServletContext().getRequestDispatcher(scenery_url + id + "/show")
                        .forward(ServletActionContext.getRequest(), ServletActionContext.getResponse());
            } else if (category.getCateId().equalsIgnoreCase(Category.POLL)) {
                ServletActionContext.getServletContext().getRequestDispatcher(poll_url + id + "/show")
                        .forward(ServletActionContext.getRequest(), ServletActionContext.getResponse());
            }
        }

        return new DefaultHttpHeaders("show").disableCaching();
    }

}





在2009-05-01,"Nils-Helge Garli Hegvik" <ni...@gmail.com> 写道:
>I'm not sure I understand your problem. Maybe you can give an example?
>I would be surprised if you couldn't handle this with regular Struts 2
>results.
>
>http://struts.apache.org/2.1.6/docs/result-types.html
>http://struts.apache.org/2.1.6/docs/result-configuration.html
>http://struts.apache.org/2.1.6/docs/parameters-in-configuration-results.html
>
>Nils-H
>
>
>2009/4/30 xnpeng <xn...@163.com>:
>> thanks for  your reply.
>> the result types cannot do my jobs in struts2+rest environment. in this environment, the "success","error",... did not work, only "index","show" ,... are the default result types. I can manage this.
>>
>> my situation is, I need to forward request to action conditionally,ie,depend on "category" in action class.
>>
>>
>>
>>
>> 在2009-04-29,"Nils-Helge Garli Hegvik" <ni...@gmail.com> 写道:
>>>Why do you need to do it manually with the request dispatcher? Can't
>>>you use any of the available result types?
>>>
>>>http://struts.apache.org/2.1.6/docs/result-types.html
>>>
>>>Nils-H
>>>
>>>2009/4/29 xnpeng <xn...@163.com>:
>>>> from:
>>>>
>>>> ServletActionContext.getServletContext().getRequestDispatcher("/post/message/"+id).forward(ServletActionContext.getRequest(),ServletActionContext.getResponse());
>>>>
>>>> I want to forward request to http://localhost:8080/post/message/3166,
>>>>
>>>> but it always throws out exception says requested resource not exist.
>>>>
>>>> when replace the string with "/index.jsp",it can work correctly.
>>>>
>>>> so, anyone who can help? I just want to forward request to an ACTION other than jsp file.
>>>>
>>>> thanks.
>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>>For additional commands, e-mail: user-help@struts.apache.org
>>>
>>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>

Re: Re: How to forward request in struts 2.1.6?

Posted by Nils-Helge Garli Hegvik <ni...@gmail.com>.
I'm not sure I understand your problem. Maybe you can give an example?
I would be surprised if you couldn't handle this with regular Struts 2
results.

http://struts.apache.org/2.1.6/docs/result-types.html
http://struts.apache.org/2.1.6/docs/result-configuration.html
http://struts.apache.org/2.1.6/docs/parameters-in-configuration-results.html

Nils-H


2009/4/30 xnpeng <xn...@163.com>:
> thanks for  your reply.
> the result types cannot do my jobs in struts2+rest environment. in this environment, the "success","error",... did not work, only "index","show" ,... are the default result types. I can manage this.
>
> my situation is, I need to forward request to action conditionally,ie,depend on "category" in action class.
>
>
>
>
> 在2009-04-29,"Nils-Helge Garli Hegvik" <ni...@gmail.com> 写道:
>>Why do you need to do it manually with the request dispatcher? Can't
>>you use any of the available result types?
>>
>>http://struts.apache.org/2.1.6/docs/result-types.html
>>
>>Nils-H
>>
>>2009/4/29 xnpeng <xn...@163.com>:
>>> from:
>>>
>>> ServletActionContext.getServletContext().getRequestDispatcher("/post/message/"+id).forward(ServletActionContext.getRequest(),ServletActionContext.getResponse());
>>>
>>> I want to forward request to http://localhost:8080/post/message/3166,
>>>
>>> but it always throws out exception says requested resource not exist.
>>>
>>> when replace the string with "/index.jsp",it can work correctly.
>>>
>>> so, anyone who can help? I just want to forward request to an ACTION other than jsp file.
>>>
>>> thanks.
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>For additional commands, e-mail: user-help@struts.apache.org
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re:Re: How to forward request in struts 2.1.6?

Posted by xnpeng <xn...@163.com>.
thanks for  your reply.
the result types cannot do my jobs in struts2+rest environment. in this environment, the "success","error",... did not work, only "index","show" ,... are the default result types. I can manage this.
 
my situation is, I need to forward request to action conditionally,ie,depend on "category" in action class.




在2009-04-29,"Nils-Helge Garli Hegvik" <ni...@gmail.com> 写道:
>Why do you need to do it manually with the request dispatcher? Can't
>you use any of the available result types?
>
>http://struts.apache.org/2.1.6/docs/result-types.html
>
>Nils-H
>
>2009/4/29 xnpeng <xn...@163.com>:
>> from:
>>
>> ServletActionContext.getServletContext().getRequestDispatcher("/post/message/"+id).forward(ServletActionContext.getRequest(),ServletActionContext.getResponse());
>>
>> I want to forward request to http://localhost:8080/post/message/3166,
>>
>> but it always throws out exception says requested resource not exist.
>>
>> when replace the string with "/index.jsp",it can work correctly.
>>
>> so, anyone who can help? I just want to forward request to an ACTION other than jsp file.
>>
>> thanks.
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>For additional commands, e-mail: user-help@struts.apache.org
>

Re: How to forward request in struts 2.1.6?

Posted by Nils-Helge Garli Hegvik <ni...@gmail.com>.
Why do you need to do it manually with the request dispatcher? Can't
you use any of the available result types?

http://struts.apache.org/2.1.6/docs/result-types.html

Nils-H

2009/4/29 xnpeng <xn...@163.com>:
> from:
>
> ServletActionContext.getServletContext().getRequestDispatcher("/post/message/"+id).forward(ServletActionContext.getRequest(),ServletActionContext.getResponse());
>
> I want to forward request to http://localhost:8080/post/message/3166,
>
> but it always throws out exception says requested resource not exist.
>
> when replace the string with "/index.jsp",it can work correctly.
>
> so, anyone who can help? I just want to forward request to an ACTION other than jsp file.
>
> thanks.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org