struts2_基础

Struts2

Result结果配置

全局结果

package标签中配置global-results标签

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

作用是为package中配置的所有action提供全局的返回结果页面

局部结果

action标签中配置result标签

<action name="xxx" method="xxx"
    class="xxx">
    <result>xxx.jsp</result>
</action>

作用是为当前action配置结果页面

结果类型

结果类型是在父类配置struts-default中定义的,struts框架默认实现了绝大多数的。
常用的结果类型有:

  1. dispatcher:转发。为result标签的默认值。

    <result type="dispatcher">xxx.jsp</result>

    由action转发到jsp。

    result标签中配置的结果必须是jsp页面

  2. chain:转发。

    <result type="chain">
        <param name="actionName">xxx.action</param>
    </result>   

    由action转发到action。

    result标签中配置的结果必须是action

  3. redirect:重定向。

    <result type="redirect">
        <param name="location">xxx.jsp</param>
    </result>

    由action重定向到jsp。

    result标签中配置的结果必须是jsp页面

  4. redirectAction:重定向。

    <result type="redirectAction">
        <param name="actionName">xxx.action</param>
    </result>   

    由action重定向到action。

    result标签中配置的结果必须是action

  5. stream: 结果为文件流。文件下载

    <result name="success" type="stream">
        <param name="contentType">image/jpeg</param>
        <param name="inputName">imageStream</param>
        <param name="contentDisposition">attachment;filename="document.pdf"</param>
        <param name="bufferSize">1024</param>
    </result>

    contentType:下载文件类型

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

    bufferSize:读文件的缓存大小

    inputName:对应要输出到客户端流声明的名称

  6. json:转发。

    <package name="xxx" namespace="/xxx" extends="json-default">
        <action name="json" method="xxx"
            class="org.itheima.struts.action.JsonAction">
            <result name="success" type="json">
                <param name="encoding">utf-8</param>
                <param name="root">jsonObject</param>
            </result>
        </action>   
    </package>

    由action转发到json结果

    encoding:配置编码格式

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

  7. jsonActionRedirect: 重定向。

    <action name="xxx" method="xxx"
            class="org.itheima.struts.action.JsonAction">
            <result name="success" type="jsonActionRedirect">
                xxx.action
            </result>
    </action>

    由action重定向到json结果。

json Plugin的加载

  1. 拷贝struts2-json-plugin-xxx.jar
  2. 让package继承json-default

Struts与Servlet交互对接

解决的问题

客户端与服务端交互时,通常会带参数过来,服务器也会回写数据给客户端。在此过程中,参与着请求,和响应,以及会话。servlet在此过程中提供了HttpServletRequest作为获取请求数据的方案,HttpServletResponse作为响应的方案,HttpSession负责了会话方案。Struts其实是基于servlet实现的web框架,他需要遵循规则提供请求,响应和会话的API供开发人员使用,因此Struts针对这一问题提供了自己的一套API封装,提供了多种方式的访问。

ActionContext

  1. ActionContext对象实例获取

    ActionContext context = ActionContext.getContext();

    ActionContext是绑定在当前线程中的。框架在请求线程开启时,将ActionContext实例对象就绑定上来了,因此我们可以通过静态方法直接获取

  2. 请求参数的获得

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

    相当于Servlet中的request.getParamters()方法

  3. 数据回显到页面

    context.put(key, value);

    相当于Servlet中的request.setAttribute()方法

ServletActionContext

  1. Servlet继承子ActionContext,因此具备ActionContext的一切功能。
  2. ServletActionContext是对ActionContext功能的扩展,提供了多个静态方法。
  3. 请求获得

    HttpServletRequest request = ServletActionContext.getRequest();
  4. 响应获得

    HttpServletResponse response = ServletActionContext.getResponse();

接口实现类获得

  • ServletContextAware
  • ServletRequestAware
  • ServletResponseAware
  • ParameterAware
  • SessionAware
  • ApplicationAware
  • PrincipalAware

Struts数据的封装

属性驱动:基本数据类型封装(掌握)

  1. 页面表单

    <input type="text" name="name">
    <input type="text" name="password">
  2. action类

    private String name;
    private String password;
    public void setName(String name) {
    this.name = name;
    }
    public void setPassword(String password) {
    this.password = password;
    }
    1. 要求页面中提供的key和action类中属性名称必须一致
    2. action类中必须提供属性的setter方法

页面表达式:对象封装

  1. 页面表单

    <input type="text" name="user.name">
    <input type="text" name="user.password">
  2. action类

    private User user;
    public void setUser(User user) {
    this.user = user;
    }
    public User getUser() {
    return user;
    }
    1. 页面中的key相当于对象.属性
    2. action中的对象名称需要要和页面中key的对象名称相同
    3. action中的对象中的属性名称要和页面中key的对象的属性相同
    4. action中的对象必须提供无参构造函数
    5. action中的对象必须提供getter和setter

模型驱动:对象封装(掌握)

  1. 页面表单

    <input type="text" name="name">
    <input type="text" name="password">
  2. Aciton类需要实现ModelDriven接口,并且实现接口方法

    public class Action03 extends ActionSupport implements ModelDriven<User> {
    private User user;
    @Override
    public User getModel() {
        if (user == null) {
            user = new User();
        }
        return user;
    }
    }

    页面中的key的名称需要和模型对象的名称一致

List数据(了解)

  1. 页面表单

    <input type="text" name="list[0].name">
    <input type="text" name="list[0].age">
    <input type="text" name="list[1].name">
    <input type="text" name="list[1].age">
  2. action类

    public class DataAction05 extends ActionSupport {
        private List<User> list;
        public List<User> getList() {
            return list;
        }
        public void setList(List<User> list) {
            this.list = list;
        }
        public String test() {
            System.out.println(list);
            return SUCCESS;
        }
    }

Map数据(了解)

  1. 页面表单

    <input type="text" name="map['a'].name">
    <input type="text" name="map['a'].age">
    <input type="text" name="map['b'].name">
    <input type="text" name="map['b'].age">
  2. action类

    public class DataAction05 extends ActionSupport {
        private Map<String, User> map;
        public Map<String, User> getMap() {
            return map;
        }
        public void setMap(Map<String, User> map) {
            this.map = map;
        }
    }

类型转换器

解决的问题

客户端传输过程中传输特定类型的字符串时,到action类中需要转换为对应的对象时,中间的转换的问题。
主要解决的是对象类型的转换

配置方案

  1. 新建一个类继承StrutsTypeConverter,实现内部方法

    public class DateConversion extends StrutsTypeConverter {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    @Override
    public Object convertFromString(Map context, String[] values, Class toClass) {
        if (values != null && values.length > 0) {
            String dateString = values[0];
            try {
                return sdf.parse(dateString);
            } catch (ParseException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }
    @Override
    public String convertToString(Map context, Object o) {
        if (o != null && o instanceof Date) {
            return sdf.format(o);
        }
        return null;
    }
    }
  2. 在项目的src下新建xwork-conversion.properties,在其中配置要转换的类型

    java.util.Date=org.itheima.struts.action.DateConversion
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值