`

struts1.x_i18n_Convert_Plugin

    博客分类:
  • ssh
阅读更多
struts1.x_i18n_Convert_Plugin

关于struts1.x中的过滤器,插件(数据类型转换),国际化的整理:
在项目的配置文件(struts-config.xml)中要添加的配置信息:
<controller processorClass="controller.CharacterController"></controller>     
    <message-resources parameter="MessageResources" />
   
<plug-in className="plugin.DateConvertPlugIn"></plug-in>

注意:在web.xml文件中 <load-on-startup>2</load-on-startup> 数值大于0表示在初始化时就读取全部配置!
一:控制器  
之——自定义过滤器:(相当于普通项目中的filter)
1, 关于控制器(之过滤器)的配置必须写在<message-resources>的后面,否则会报错!
<controller processorClass="controller.CharacterController"></controller> 
    processorClass属性指定相应的定义控制器的全类名
2, 控制器之过滤器类继承自RequestProcessor类,重写processPreprocess()方法(@Override)

public class CharacterController extends RequestProcessor {
@Override
protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub

System.out.println("struts 控制器已经加入");

try {
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return super.processPreprocess(request, response);
}
}

二,插件:
(自定义数据类型转换的插件——往actionForm中封装的数据类型为引用数据类型)
1,配置:<plug-in className="plugin.DateConvertPlugIn"></plug-in>
className属性指定实现插件类的全类名
2,插件类实现PlugIn接口,在init()中进行相关操作。
public class DateConvertPlugIn implements PlugIn {

public void destroy() {
// TODO Auto-generated method stub

}

public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {
System.out.println("日期转换器开始工作。。。");
ConvertUtils.register(new UtilDateConvter(), java.util.Date.class);

}
}
3,实现了插件的数据转换功能的类,实现Converter接口
(根据所需要添加的插件的功能去实现相应功能的接口,如,这里添加的插件是为了进行数据类型转换,故而实现了Converter接口)
public class UtilDateConvter implements Converter {

public Object convert(Class arg0, Object text) {
// TODO Auto-generated method stub
Date date=null;

if(text==null)
return (null);
if(text instanceof Date)
return text;
if(text instanceof String)
{

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
try {
date= sdf.parse((String)text);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return date;
}

}


三,国际化:
1, 为使用国际化而往工具中添加国际话的相应插件的步骤:
a) 将插件文件夹plugins拷到工具的目录下
b) 将configuration 中的文件全部删除只留下一个config.ini(配置设置文件)

2, 具体操作:
a) 国际化文件(MessageResources.properties)名以.properties结尾,名称以配置文件
<message-resources parameter="MessageResources" />中的parameter属性开始以“_”连接相应的语言标识(默认文件不加)
b)在静态页面中需要使用struts的bean标签(<bean:message key="login.uname"/>),使用taglib指令引入相应的标签支持(相应的错误提示需要引入html标签)
c)  在<bean:message key="login.uname"/>中,用key指定要使用的静态内容对应的文件中key的全名(错误提示使用<html:errors/>,通过<html:errors property="password_error"/>中的property指定error名称)
<body>
  <html:errors/>
    <form action="users.do" method="post" name="myform">
    用户ID:<input type="text" name="users.uid"/><br/>
    <bean:message key="login.uname"/><input type="text" name="users.uname"/><br/>
    <bean:message key="login.upass"/><input type="text" name="users.upass"/>
<html:errors property="password_error"/><br/>
    登录时间:<input type="text" name="users.loginTime" /><br/>
    <input type="hidden" name="method" value="dologin"/>
    <input type="submit" value='<bean:message key="login.submit"/>'/>
    </form>
  </body>
3,关于国际化中的报错机制:

要实现error错误处理,根据需要在,配置文件中配置或程序中编码控制:
a)在国际化文件中配置:login.upass.error = The Length of password must be 3
b),在程序中代码控制:
error继承自ActionMessages,是错误信息的管理容器
往容器中添加错误信息:errors.add(String name, ActionMessage arg0);
errors.add("login_error", new ActionMessage("login.fail")); super.addErrors(request, errors);//将错误信息存储在域中。
b) 关联关系:

Name属性用来关联.properties文件中的key(new ActionMessage("login.fail"))
(name对应<html:errors property="password_error"/> 中的property,什么都不写,为全部信息,写了,就是某个信息)


代码:

页面中:
<html:errors/>
<html:errors property="password_error"/>

类中:
ActionMessages errors=new ActionMessages();//实例化错误信息管理容器
if(users.getUpass().length()<=3)
{
errors.add("password_error", new ActionMessage("login.upass.error"));//往容器中添加错误信息
}

if(!users.getUname().equals("admin") || !users.getUpass().equals("accp"))
{
errors.add("login_error", new ActionMessage("login.fail"));
}


if(errors.isEmpty())
{
return mapping.findForward("success");
}
super.addErrors(request, errors);
return mapping.findForward("fail");

错误提示机制的理解:
ActionMessages errors=new ActionMessages();//实例化错误信息管理容器
errors.add(String name, ActionMessage arg0);//使用容器关联error 和.properties
super.addErrors(request, errors);//存储

在页面中通过<html:errors/>
<html:errors property="password_error"/>来获得错误信息,是通过property关联到java类中的ActionMessages容器,该容器将每一个property指定一个.properties文件中的key通过new的方式加载

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics