Struts2——Result处理&获取页面请求参数&API

一、Result处理

1.1 说明

  平常我们设置跳转页面,是在action标签里面加上 result标签来控制,这种设置的页面跳转,称之为 局部结果页面;但是我们有时候在很多个action里面,针对不同的结果进行跳转时,也有可能跳转同一个页面,那么这个时候就可以配置全局结果页面。

1.2 局部结果配置eg

<action name="demo01_*" class="com.gaga.web.action.ActionDemo01" method="{1}">     <result name="error">/error.jsp</result></action>

1.3 全局结果配置eg(同一个包下)

<package name="test" extends="struts-default" namespace="/">    <global-results>        <result name="error">/error.jsp</result>    </global-results>      ....</package>

1.4全局结果配置eg(不同的包下)

    <!--把全局的结果抽取到父包里面  -->    <package name="base" extends="struts-default" abstract="true">        <global-results>            <!--全局的结果: 配置在package里面的 特点:可以被当前包里面所有的Action共用; 使用场景:不同的Action需要跳转到相同的结果的时候eg: 错误页面, msg页面等  -->            <result name="error">/msg.jsp</result>        </global-results>    </package>    <package name="test" extends="base" namespace="/">        <action name="demo01_fun01" class="com.gaga.web.ActionDemo01" method="fun01">        </action>        <action name="demo02_fun02" class="com.gaga.web.ActionDemo02" method="fun02">        </action>    </package>

1.5 结果的类型

  服务器响应给浏览器的时候,有三种类型:response响应JSON数据&请求转发 & 重定向。 对于Struts2而言,无非就是Action跳转(转发重定向)到页面,Action跳转到Action….

1.5.1 Action跳转页面

  转发(默认)

<result name="success" type="dispatcher">/index.jsp</result>

  重定向

<result name="success" type="redirect">/index.jsp</result>

1.5.2 Action跳转Action

 转发

<action name="demo01_*" class="com.gaga.web.action.ActionDemo01" method="{1}">        <result name="success" type="chain">demo02_fun02</result></action><action name="demo02_*" class="com.gaga.web.action.ActionDemo02" method="{1}"></action>

 重定向 

<action name="demo01_*" class="com.gaga.web.action.ActionDemo01" method="{1}">        <result name="success" type="redirectAction">demo02_fun02</result></action><action name="demo02_*" class="com.gaga.web.action.ActionDemo02" method="{1}"></action>

1.5.4 其它的结果(用得不多)

  一般来说,我们平常的请求到来,我们要不就是跳转到 页面上 ,要不就是跳转到下一个action 去。 但是除了这两种结果之外,我们仍然还有一些其他结果类型可以控制.

?   比如: 我们可以返回一个文件数据给客户端 (比如文件下载).再比如: 我们可以返回一个json字符串给来请求的页面,而不是重新打开新的页面 (有点像之前的Ajax请求,返回json数据)

1.5.4.1响应JSON数据

导入struts-json-plugin-xxx.jarjava代码

public class ActionDemo01 extends ActionSupport {    private User json;        public User getJson() {        return json;    }    public String fun01(){        json = new User();        json.setName("张三");        json.setAge(18);        json.setPassword("123456");                return "success";    }    }

配置文件

<struts>    <package name="test" extends="json-default" namespace="/">        <action name="demo01" class="com.itheima.web.action.ActionDemo01" method="fun01">            <result name="success" type="json">                                <param name="root">json</param><!--这里的name必须是root  至于这个json 是我们在action里面的成员 变量 json(属性)  -->             </result>        </action>    </package></struts>

  备注:

  root:配置对象。action类中必须提供一个和root值相同的属性名称,且需要提供getter方法。

?   package需要继承json-default

?   result的type值是json

param标签中的内容必须为Action类中的Javabean属性,该例子中,由于拿取User对象json的方法为getJson(),把get去掉,J变为小写,即为json,那么就有了<param name=”root”>json</param>

1.5.4.2响应流(文件下载)

java代码

public class ActionDemo01 extends ActionSupport {    private InputStream stream;        public void setStream(InputStream stream) {        this.stream = stream;    }    public InputStream getStream() {        return stream;    }    public String fun01() throws Exception{        System.out.println("demo01 执行了...");        stream = new FileInputStream("E:/data/Desktop/a.jpg");                return "success";    }}

配置文件

<struts>    <package name="test" extends="struts-default" namespace="/">        <action name="demo01" class="com.itheima.web.action.ActionDemo01"            method="fun01">            <result name="success" type="stream">                <param name="contentType">image/jpeg</param>                <param name="inputName">stream</param>                <param name="contentDisposition">attachment;filename="b.jpg"</param>                <param name="bufferSize">1024</param>            </result>        </action>    </package></struts>

  说明  

  contentType:下载文件类型

?   contentDisposition:下载到客户端时,客户端文件名称

?   bufferSize:读文件的缓存大小

?   inputName:对应要输出到客户端流声明的名称,也就是说需要和Action里面声明的变量名要一致

二、利用Struts2获取页面参数

1. 获取零散数据(即封装前的数据)

1.1 利用Struts2中的API

利用ActionContext(Action中的上下文)

//1.创建ActionContext对象ActionContext context = ActionContext.getContext();//2.获得所有的请求参数Map<String, Object> parameters = context.getParamters();

利用ServletActionContext

//1.获得request对象HttpServletRequest request = ServletActionContext.getRequest();//获取具体参数requet.getParameter(String key);requet.getParameterValues(String key);requet.getParameterMap();

1.2 利用属性驱动

页面

<h1>01使用属性驱动</h1><form method="post" action="${pageContext.request.contextPath }/demo01">    用户名:<input type="text" name="username"/><br/>    密    码:<input type="password" name="password"/><br/>    <input type="submit"/></form>

Action.java

public class ActionDemo01 extends ActionSupport {    private String username;//和表单里面的name属性值要一致,并且提供set方法    private String password;//和表单里面的name属性值要一致,并且提供set方法    public void setUsername(String username) {        this.username = username;    }    public void setPassword(String password) {        this.password = password;    }    public String fun01(){        System.out.println(username+":"+password);        return NONE;    }}

2. 直接获取封装后的数据(即直接获取对象)

2.1 属性驱动

页面

<h1>01使用属性驱动方式</h1><form method="post" action="${pageContext.request.contextPath }/demo01">    用户名:<input type="text" name="user.username"/><br/>    密    码:<input type="password" name="user.password"/><br/>    <input type="submit"/></form>

Action类

public class ActionDemo01 extends ActionSupport {    //1. User类里面的字段属性需要和表单里面的name属性一致, 且提供无参构造      //2. user需要set和get方法    private User user;    public User getUser() {        return user;    }    public void setUser(User user) {        this.user = user;    }    public String fun01(){        System.out.println(user.toString());        return NONE;    }}

2.2 模型驱动

页面

<h1>02使用模型驱动方式</h1><form method="post" action="${pageContext.request.contextPath }/demo02">    用户名:<input type="text" name="username"/><br/>    密    码:<input type="password" name="password"/><br/>    <input type="submit"/></form>

Action类

public class ActionDemo02 extends ActionSupport implements ModelDriven<User> {        private User user;        public String fun02(){        System.out.println(user.toString());        return NONE;    }    @Override    public User getModel() {        if(user == null){            user = new User();        }        return user;    }}

  经验:实际开发中,

如果要获得单个(零散)的数据,我们通常用属性驱动

如果要获得封装后的数据, 我们通常用模型驱动

2.3 封装到List集合(用得不多)

页面

<h1>01封装到list</h1><form method="post" action="${pageContext.request.contextPath }/demo01">    用户名:<input type="text" name="list[0].username"/><br/>    密    码:<input type="password" name="list[0].password"/><br/>    用户名:<input type="text" name="list[1].username"/><br/>    密    码:<input type="password" name="list[1].password"/><br/>    <input type="submit"/></form>

Action类

public class ActionDemo01 extends ActionSupport {        private List<User> list;        public List<User> getList() {        return list;    }    public void setList(List<User> list) {        this.list = list;    }    public String fun01(){        System.out.println(list.toString());        return NONE;    }}

2.4 封装到Map中

页面

<h1>02封装到map</h1><form method="post" action="${pageContext.request.contextPath }/demo02">    用户名:<input type="text" name="map['user1'].username"/><br/>    密    码:<input type="password" name="map['user1'].password"/><br/>    用户名:<input type="text" name="map['user2'].username"/><br/>    密    码:<input type="password" name="map['user2'].password"/><br/>    <input type="submit"/></form>

Action类

public class ActionDemo02 extends ActionSupport {        private Map<String, User> map;        public Map<String, User> getMap() {        return map;    }    public void setMap(Map<String, User> map) {        this.map = map;    }    public String fun02(){        Set<Entry<String, User>> entrySet = map.entrySet();        for (Entry<String, User> entry : entrySet) {            System.out.println(entry.getKey()+":"+entry.getValue().toString());        }        return NONE;    }

三、补充:Struts2中的API访问

3.1 用ActionContext

  ActionContext是Action的上下文,Struts2自动在其中保存了一些在Action执行过程中所需的对象,比如session, parameters等。Struts2会根据每个执行HTTP请求的线程来创建对应的ActionContext,即一个线程有一个唯一的ActionContext。

创建对象

ActionContext context = ActionContext.getContext();

获取请求参数

Map<String, Object> parameters = context.getParamters();

3.2 用ServletActionContext

  ServletActionContext继承ActionContext,因此比ActionContext功能要强大。ServletActionContext提供了多个静态方法。

//获得Request对象HttpServletRequest request = ServletActionContext.getRequest();//获得Response对象HttpServletResponse response = ServletActionContext.getResponse();//获得ServletContextServletContext servletContext = ServletActionContext.getServletContext();

3.3 通过实现接口

  有如下接口可选

? ServletContextAware

? ServletRequestAware

? ServletResponseAware

? SessionAware

? ApplicationAware

eg:

public class ActionDemo03 extends ActionSupport implements ServletRequestAware {    private HttpServletRequest request;    public String fun03() throws IOException{        HttpServletRequest request = ServletActionContext.getRequest();        String username = request.getParameter("username");        String password = request.getParameter("password");        System.out.println(username+":"+password);        return NONE;    }    @Override    public void setServletRequest(HttpServletRequest request) {        this.request = request;    }}

只有不快的斧,没有劈不开的柴。

Struts2——Result处理&amp;获取页面请求参数&amp;API

相关文章:

你感兴趣的文章:

标签云: