如何在Struts 2中访问请求? [英] How do I access the request in Struts 2?

查看:59
本文介绍了如何在Struts 2中访问请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对jsp页面中的某些操作进行对操作类的AJAX后期调用.

I am doing an AJAX post call to the action class on some action in the jsp page.

但是我不知道如何在我的操作类中访问该请求,或者如何为该请求设置响应,该响应将被发送回调用JSP.

But I do not know how to access the request in my action class, or how to set the response for that request which will be sent back to the calling JSP.

我还想知道我是否必须以不同的方式为该异常动作编写代码吗?这意味着对于这些类型的操作, struts.xml 中应该包含什么.

Also I want to know whether i have to write code for that perticular action in a different way? That means what should be there in struts.xml for these types of actions.

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "struts.apache.org/dtds/struts-2.0.dtd">; 
 <struts> 
   <package name="default" extends="struts-default">
      <action name="populateDD" class="com.struts.action.FilterData"  
                method="populateDD">    
        <result name="success">/index.jsp</result>
 </action> 
</package>
</struts>

推荐答案

Struts 2不在乎请求是否为Ajax.

Struts 2 does not care if a request is Ajax or not.

只要有可能,您就不要依赖servlet规范工件,而应使用默认的Struts 2功能,该功能自动将请求参数和请求属性映射到动作属性或从动作属性映射.

Whenever possible, you should not rely on servlet spec artifacts and instead use the default Struts 2 functionality of automatically mapping request parameters, and request attributes, to and from action properties.

例如,如果您通过Ajax发布了名称"参数,并希望基于该参数返回某内容,则该内容将类似于以下内容:

For example, if you posted a "name" parameter via Ajax, and wanted to return something based on that, it'd look something like this:

public class MyAction extends ActionSupport {
    private String name; // Plus public getter and setter--from the request.
    private String someReturnValue; // Plus public getter and setter--for the view layer

    public String execute() {
        someReturnValue = "Your name is " + name;
        return SUCCESS;
    }
}

在JSP中,您可以通过Struts 2标记进行访问,也可以仅使用JSP EL:

In the JSP you can access via the Struts 2 tags, or just use JSP EL:

<p>Return value is ${someReturnValue}</p>

如果您真的需要访问

If you really need access to the request (FAQ) or response (FAQ) (and you rarely do), you can.

这篇关于如何在Struts 2中访问请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆