码迷,mamicode.com
首页 > 其他好文 > 详细

struts2 学习记录 过滤器 国际化

时间:2014-08-18 23:20:13      阅读:371      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   io   文件   

  struts2接触不是一天两天了,但是一直没有用它做什么项目,但老师确一直说它有很大的学习价值,所以还是把我学习到的东西给记录一下,记录的东西没有规律,只是给自己留个备份,

  struts2中最关键的是action,action可以用来传值,用起来很方便,它可以用el表达式在jsp页面上展示出来,也可以用ognl,el只能表达作用域里面的值,而struts2里面还有个放数据的地方叫做ActionContext,这个类里面可以放不在作用域里的键值对,但是el表达式还是可以获取到,但是效率很低,所以尽量不要用。

  关于el表达式和ognl表达式的区别,网上已经有很多的资料了,想了解的可以去查查看,我今天想说的是struts的拦截器和i18n,先说拦截器吧!

  之前说过过滤器,我觉得,拦截器和过滤器的没理没有什么不同,都是截获请求,然后对其进行处理,struts的拦截器拦截的是访问action的请求,即使我们没有配置struts的拦截器,它也要经过很多拦截器才能到达想要访问的action,这个配置是struts的默认配置,我们引入struts的jar包关联源码后,在struts2-core-2.3.16.jar下面的org.apache.struts2.default.properties这个文件,里面有我们要配置的所有struts.xml的选项和默认值,当然过滤器的配置也可以从它这里边参考写法,struts的过滤器有依赖注入的作用,也有和Servlet,httpServletRequest,httpServletResponse解藕的作用,这些都是过滤器来起作用的,所以我们自己配置自己的过滤器时也要把默认的过滤器加上,否则会出现很多问题,比如action里可能会得不到值等等,接下来就是写代码的工作了,写一个自己的struts的过滤器要实现Interceptor接口,下面的代码是一个登录页面的例子,

package com.bjsxt.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class LoginInterceptor implements Interceptor{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3153545565744347773L;

	@Override
	public void destroy() {
		System.out.println("end");
	}

	@Override
	public void init() {
		System.out.println("start");
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println(invocation.getProxy().getActionName());
		System.out.println(invocation.getProxy().getMethod());
		System.out.println(invocation.getProxy().getNamespace());
		System.out.println(invocation.getProxy().getAction());
		@SuppressWarnings("unchecked")
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		if(invocation.getInvocationContext().getSession().get("user")!=null){
			return invocation.invoke();
		}else{
			if("login".equals(invocation.getProxy().getMethod())&&"UserAction".equals(invocation.getProxy().getActionName())){
				return invocation.invoke();
			}else{
				request.put("msg", "login the system first!!!");
				return "login";
			}
		}
	}

}

  接下来是配置struts.xml

代码如下:

<package name="default" namespace="/" extends="struts-default">
    <interceptors>
    	<interceptor name="myInterceptor" class="com.bjsxt.interceptor.MyInterceptor"></interceptor>
    	<interceptor name="loginInterceptor" class="com.bjsxt.interceptor.LoginInterceptor"></interceptor>
    	<interceptor-stack name="myInter">
    		<interceptor-ref name="defaultStack"></interceptor-ref>
    		<interceptor-ref name="loginInterceptor"></interceptor-ref>
    		<interceptor-ref name="myInterceptor"></interceptor-ref>
    	</interceptor-stack>
    </interceptors>
    
    <default-interceptor-ref name="myInter"></default-interceptor-ref>
    
    <global-results>
    	<result name="login">/login.jsp</result>
    </global-results>
    
        <action name="user" class="com.bjsxt.action.UserAction" >
        	<result name="SUCCESS">/result.jsp</result>
        	<result name="FAIL">/fail.jsp</result>
        	<result name="search">/list.jsp</result>
        	<result name="change">/change.jsp</result>
        </action>

      <action name="test" class="com.bjsxt.action.TestAction" >
      <result name="SUCCESS">/list.jsp</result>
    </action>

    <action name="UserAction" class="com.bjsxt.action.UserAction" method="login">
      <result name="login" >/login.jsp</result>
      <result name="SUCCESS" >/loginseccess.jsp</result>
    </action>

 </package>
</struts>

  从配置文件里可以看出来,把默认的配置给加上了,这里主要是讲过滤器,所以action和jsp的代码略,接下来是测试截图,其它页面直接跳转到登录页面,而登录页面可以登录。

 

bubuko.com,布布扣

bubuko.com,布布扣

 

 

接下来是国际化,如果一个系统想要被多个国家的人所用就不得不考虑国际化的问题,如果为每一个国家都开发一套系统维护起来会很麻烦,所以就有了个思想,那就是把动态的,用不同语言展示的部分,放到配置文件中,动态的从文件里读取想要展示的内容,不同国家的人就得到了不同的展示,这是一个很好的思想,所以我把它记录一下了,嘻嘻。。。

  首先要把它的配置文件写好,以中文和美式英语为例,配置文件如下:

配置文件是以键值对的形式存在的,且以properties结尾,命名也有一定的格式,如test_zh_CN.properties这个是中文的文件名字,_zh_CN.properties这些都是不能变的,且大小写也要一致,而test是它的命名,是在配置文件中出现的代表这个文件的名字,而英文的是test_en_US.properties,截图如下:

bubuko.com,布布扣

bubuko.com,布布扣

接下来是struts.xml,要在配置文件中加上这句配置<constant name="struts.custom.i18n.resources" value="test"></constant>,test就是上面两个配置文件的名字,所以上面两个配置文件的名字一定要一样哦,接下来是测试的代码

package com.bjsxt.test;

import java.util.Locale;
import java.util.ResourceBundle;

public class test {
    public static void main(String[] args) {
        
        ResourceBundle rb = ResourceBundle.getBundle("test", Locale.US);
        String value = rb.getString("welcome");
        System.out.println(value);
    }
}

结果为welcome to my house

把上面的代码中的

 ResourceBundle rb = ResourceBundle.getBundle("test", Locale.US);改为
 ResourceBundle rb = ResourceBundle.getBundle("test", Locale.CHINA);结果变为欢迎来到我的小屋


测试页面如下所示,先说中文的,

打开浏览器,发布项目,输入地址,页面如下所示:

bubuko.com,布布扣

接下来要将浏览器设置为英文的,以firefox为例,在菜单栏的右上角点击打开菜单,然后点击选项,不过可能浏览器的版本不同,位置也会不同,找不到的可以百度,界面如下所示

bubuko.com,布布扣

然后点击选择,将英语/美国上移到最上面,点确定

bubuko.com,布布扣

然后再访问之前的地址,就会显示如下所示的内容。

bubuko.com,布布扣

也可以在页面中放置动态的内容哦,不过要设置点位符,在这里就不再详细的写了,网上应该会有很多的资料,感兴趣的可以去找找看哦!!!!,今天就先总结到这里了吧




























struts2 学习记录 过滤器 国际化,布布扣,bubuko.com

struts2 学习记录 过滤器 国际化

标签:des   style   blog   http   color   java   io   文件   

原文地址:http://www.cnblogs.com/lilyjia/p/3916268.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!