(三)Struts1.x高级应用

来源:互联网 发布:淘宝买二手手机可靠吗 编辑:程序博客网 时间:2024/05/22 16:40

一、国际化

1、什么是国际化

按照我个人的理解,国际化就是根据客户端的语言环境显示不同的语言。也就是服务器根据客户端提交过来的语言环境返回不同的内容(客户端语言环境会在请求头“Accept-Language”中提交过来)。

2、Strut1.x国际化的使用

(1)、提供不同的资源包

资源包的命名格式:包名_语言简写_国家简写.properties(如:languang_zh_CN.properties),资源包内的内容还是key=value的形式,每个资源包的包名和key都一样,就语言这一块会不同,还有value则不同的语言的value,结构和代码如下所示:
这里写图片描述

如果资源包不能适配客户端提交过来的语言环境,则默认显示包名.properties(此处是language.properties),几个包的代码分别如下:

- language.properties:

account=default_account

- language_en_US.properties

account=en_account

- language_zh_CN.properties

account=zh_account

接下来,要在struts的配置文件将资源包配进去,代码如下:

这里写图片描述

为了使用不同的资源包,需要设置key选项(不设key的就是默认选项,都不设key就只能使用最后一项),key对应jsp页面标签中的bundle,jsp页面代码如下:

<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %><%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %><html><head>    <title>language</title></head><body><html:messages id="account" message="true" bundle="language">    <bean:write name="account"></bean:write></html:messages></body></html>

接下来配合Action里的代码,讲述jsp相关属性与Action代码相关属性的关系:

public class LanguageAction extends Action {    @Override    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {        ActionMessage message = new ActionMessage("account");        ActionMessages messages = new ActionMessages();        messages.add("message", message);        this.addMessages(request, messages);        return mapping.findForward("language");    }}

jsp页面中,“html:messages”标签中的bundle属性对应之前配置文件中的key,指使用哪个资源包。id属性指使用资源文件中那个key的值,与ActionMessage构造函数中的key属性一致。

二、动态FormBean

1、什么是动态FormBean

动态FormBean,说白了就是在strut配置文件中配置FormBean。众所周知,FormBean是用来封装客户端提交过来的数据的,前几篇blog中都是用一个FormBean类来接收用户数据,而动态FormBean则是一个可配置的FormBean,极大地方便了代码编写。

2、Strut1.x中如何使用动态FormBean

在Struts1.x的核心包中有org.apache.struts.action.DynaActionForm(不可校验,org.apache.struts.validator.DynaValidatorForm则可校验),我们配置FormBean时就需要在配置文件的“form-bean”标签中配入DynaActionForm,然后在它的子标签下配入各种属性,代码如下:

<form-beans>        <form-bean name="showForm" type="org.apache.struts.action.DynaActionForm">            <form-property name="name" type="java.lang.String"></form-property>            <form-property name="age" type="java.lang.Integer"></form-property>        </form-bean>    </form-beans>

接下来在Action中直接调用即可,调用代码如下:

public class ShowAction extends DispatchAction{    public ActionForward showUsers(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {        DynaActionForm deform = (DynaActionForm) form;        System.out.println(deform.get("name"));        ActionMessages messages = new ActionMessages();        ActionMessage message = new ActionMessage("age", deform.get("age"));        messages.add("message", message);        this.addMessages(request, messages);        return mapping.findForward("success");    }}

前端页面之前通过“html:messages”和“bean:write”标签获取即可。不过在这里有个困扰了我很久的问题,就是总提示在默认的bundle找不到age,原因是“html:messages”没有设置bundle选项,导致系统从默认的资源文件查找。所以我们还要通过bundle指定一个包含“age”键的资源文件。

三、DispatchAction

可能有人已经发现了Action的缺点,Action只有execute一个执行方法,要是需要增删改查操作不就要写4个Action了吗?不符合历史发展规律啊。这时DispatchAction就应运而生了,通过继承它我们可以在Action中写多个方法,就像上述代码一样,执行的方法不是execute了吧,接下来我们从访问和配置这两方面讲述它的使用。

说多无益,先贴配置代码:

<action path="/show" type="com.struts.action.ShowAction" name="showForm" parameter="method">            <forward name="success" path="/show.jsp"></forward>            <forward name="fail" path="/login.jsp"></forward>        </action>

很明显的区别,这个配置“action”标签中多了一个parameter属性,这个属性有什么用呢?很简单,我们从前端请求过来肯定要指定调用哪个方法的,而怎么获取请求参数呢?答案就是通过parameter属性,parameter属性的值正是前端提交的请求参数的键。好像说得有点懵了,举个栗子:上面代码parameter=“method”,所以前端请求就要有一个名为method的参数指定访问某个方法,此处的请求URL是“http://localhost/show?method=showUsers”,就指定了我要访问showUsers这个方法。so easy~

四、异常处理

异常处理就是当程序产生exception时需要进行的代码执行。接下来说一下全局异常的使用步骤:

  • 1、编写异常处理类,需要继承ExceptionHandler
package com.struts.exceptionHandler;import org.apache.struts.action.*;import org.apache.struts.config.ExceptionConfig;import org.apache.struts.util.ModuleException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Created by 程序猿 on 2017/5/17 0017. */public class ShowExceptionHandler extends ExceptionHandler {    @Override    public ActionForward execute(Exception ex, ExceptionConfig ae, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response) throws ServletException {        ActionMessage message = new ActionMessage("exception");        ActionForward forward = mapping.getInputForward();        this.storeException(request,"exception" ,message, forward, ae.getScope());        return mapping.getInputForward();    }}
  • 2、在struts配置文件中配置
<global-exceptions>        <exception key="exception"                   type="java.lang.ArithmeticException"                   handler="com.struts.exceptionHandler.ShowExceptionHandler"        />    </global-exceptions>

接下来,如果发生ArithmeticException异常,就会跳转到input属性的页面,在前端页面通过“html:errors”标签显示出来了。

原创粉丝点击