资源文件:
过滤器Lfilte实现中英文切换(过滤的是*.jsp和*do啊 记住 )
ApplicationResources_en_US.properties:
owner.greet = Greet
owner.name = Name
owner.address = Address
owner.email = Email
owner.tel = Tel
ApplicationResources_zh_CN.properties
owner.greet = \u79f0\u547c
owner.name = \u59d3\u540d
owner.address = \u5730\u5740
owner.email = \u7535\u5b50\u90ae\u4ef6
owner.tel = \u8054\u7cfb\u7535\u8bdd
owner.greet = Greet
owner.name = Name
owner.address = Address
owner.email = Email
owner.tel = Tel
ApplicationResources_zh_CN.properties
owner.greet = \u79f0\u547c
owner.name = \u59d3\u540d
owner.address = \u5730\u5740
owner.email = \u7535\u5b50\u90ae\u4ef6
owner.tel = \u8054\u7cfb\u7535\u8bdd
过滤器Lfilte实现中英文切换(过滤的是*.jsp和*do啊 记住 )
import
javax.servlet.
*
;
import javax.servlet.http. * ;
import java.io. * ;
import java.util. * ;
import org.apache.struts.Globals;
public class Lfilte extends HttpServlet implements Filter {
private FilterConfig filterConfig;
//Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
//Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) {
try {
HttpServletRequest hReq = (HttpServletRequest) request;
HttpSession session = hReq.getSession();
String language = request.getParameter("language");
if(language!=null)
{
if (language.equals("en")) {
session.setAttribute(Globals.LOCALE_KEY,
Locale.ENGLISH);
} else {
session.setAttribute(Globals.LOCALE_KEY,
Locale.CHINA);
}
}
filterChain.doFilter(request, response);
} catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
}
//Clean up resources
public void destroy() {
}
}
JSP显示页面
import javax.servlet.http. * ;
import java.io. * ;
import java.util. * ;
import org.apache.struts.Globals;
public class Lfilte extends HttpServlet implements Filter {
private FilterConfig filterConfig;
//Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
//Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) {
try {
HttpServletRequest hReq = (HttpServletRequest) request;
HttpSession session = hReq.getSession();
String language = request.getParameter("language");
if(language!=null)
{
if (language.equals("en")) {
session.setAttribute(Globals.LOCALE_KEY,
Locale.ENGLISH);
} else {
session.setAttribute(Globals.LOCALE_KEY,
Locale.CHINA);
}
}
filterChain.doFilter(request, response);
} catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
}
//Clean up resources
public void destroy() {
}
}
<%
@ page contentType
=
"
text/html;charset=UTF-8
"
%>
<% @ taglib uri = " /WEB-INF/displaytag-11.tld " prefix = " display " %>
<% @ taglib uri = " /WEB-INF/struts-bean.tld " prefix = " bean " %>
<% @ taglib uri = " /WEB-INF/struts-html.tld " prefix = " html " %>
<% @ taglib uri = " /WEB-INF/fmt.tld " prefix = " fmt " %>
<% @ taglib uri = " /WEB-INF/displaytag-11.tld " prefix = " display " %>
<% @ page import = " org.displaytag.sample.*, java.util.*,org.displaytag.tags.TableTag " %>
<% @page import = " org.apache.struts.Globals,java.util.* " %>
<% @ include file = " inc/init.jsp " %>
<%
// response.ge
%>
< html:html locale = " true " >
< head >
< meta http - equiv = " Pragma " content = " no-cache " />
< meta http - equiv = " Cache-Control " content = " no-cache " />
< link rel = " stylesheet " href = " css/screen.css " type = " text/css " media = " screen, print " />
< title > Struts Form for ownerForm </ title >
</ head >
< body >
< html:form action = " /owner " >
< input type = " hidden " name = " formAction " value = " changeLanguage " />
< input type = " hidden " name = " language " />
< bean:message key = " owner.greet " /> :
< html:select property = " greet " >
< html:option value = "" ></ html:option >
< html:option value = " Mr. " > Mr. </ html:option >
< html:option value = " Miss. " > Miss. </ html:option >
< html:option value = " Mrs. " > Mrs. </ html:option >
</ html:select >< html:errors property = " greet " />
</ br >
< bean:message key = " owner.name " /> : < input type = " text " name = " name " />< br >
< bean:message key = " owner.address " /> : < input type = " text " name = " address " /></ br >
< bean:message key = " owner.email " /> : < input type = " text " name = " email " /></ br >
< bean:message key = " owner.tel " /> : < input type = " text " name = " tel " /></ br >
当前语言: <%= session.getAttribute(Globals.LOCALE_KEY) %>
</ br >
< html:submit />< html:reset />
< a href = " index.jsp?language=en " > 英文版 </ a >
< a href = " index.jsp?language=cn " > 中文版 </ a >
< input type = " button " name = " langEn " value = " 英文版 " onclick = " changeLanguage('en') " />
< input type = " button " name = " langCn " value = " 中文版 " onclick = " changeLanguage('cn') " />
</ html:form >
<% Object foo = session.getAttribute( " details " );
if ( foo == null ) {
session.setAttribute( "details", new TestList(100, false) );
}
request.setAttribute( " testparam " , " sendamail " );
request.setAttribute( " test " , new TestList( 100 , false ) );
%>
< fmt:setLocale value = " <%=session.getAttribute(Globals.LOCALE_KEY)%> " />
< fmt:bundle basename = " ApplicationResources " >
< display:table name = " sessionScope.details " pagesize = " 10 " export = " true " class = " its " decorator = " org.displaytag.sample.Wrapper " >
< display:column property = " link1 " titleKey = " owner.tel " />
< display:column property = " email " titleKey = " owner.email " />
< display:column property = " link2 " title = " Actions " />
< display:column property = " longDescription " media = " csv excel xml pdf " title = " Not On HTML " />
</ display:table >
</ fmt:bundle >
< br >
</ body >
</ html:html >
<% @ taglib uri = " /WEB-INF/displaytag-11.tld " prefix = " display " %>
<% @ taglib uri = " /WEB-INF/struts-bean.tld " prefix = " bean " %>
<% @ taglib uri = " /WEB-INF/struts-html.tld " prefix = " html " %>
<% @ taglib uri = " /WEB-INF/fmt.tld " prefix = " fmt " %>
<% @ taglib uri = " /WEB-INF/displaytag-11.tld " prefix = " display " %>
<% @ page import = " org.displaytag.sample.*, java.util.*,org.displaytag.tags.TableTag " %>
<% @page import = " org.apache.struts.Globals,java.util.* " %>
<% @ include file = " inc/init.jsp " %>
<%
// response.ge
%>
< html:html locale = " true " >
< head >
< meta http - equiv = " Pragma " content = " no-cache " />
< meta http - equiv = " Cache-Control " content = " no-cache " />
< link rel = " stylesheet " href = " css/screen.css " type = " text/css " media = " screen, print " />
< title > Struts Form for ownerForm </ title >
</ head >
< body >
< html:form action = " /owner " >
< input type = " hidden " name = " formAction " value = " changeLanguage " />
< input type = " hidden " name = " language " />
< bean:message key = " owner.greet " /> :
< html:select property = " greet " >
< html:option value = "" ></ html:option >
< html:option value = " Mr. " > Mr. </ html:option >
< html:option value = " Miss. " > Miss. </ html:option >
< html:option value = " Mrs. " > Mrs. </ html:option >
</ html:select >< html:errors property = " greet " />
</ br >
< bean:message key = " owner.name " /> : < input type = " text " name = " name " />< br >
< bean:message key = " owner.address " /> : < input type = " text " name = " address " /></ br >
< bean:message key = " owner.email " /> : < input type = " text " name = " email " /></ br >
< bean:message key = " owner.tel " /> : < input type = " text " name = " tel " /></ br >
当前语言: <%= session.getAttribute(Globals.LOCALE_KEY) %>
</ br >
< html:submit />< html:reset />
< a href = " index.jsp?language=en " > 英文版 </ a >
< a href = " index.jsp?language=cn " > 中文版 </ a >
< input type = " button " name = " langEn " value = " 英文版 " onclick = " changeLanguage('en') " />
< input type = " button " name = " langCn " value = " 中文版 " onclick = " changeLanguage('cn') " />
</ html:form >
<% Object foo = session.getAttribute( " details " );
if ( foo == null ) {
session.setAttribute( "details", new TestList(100, false) );
}
request.setAttribute( " testparam " , " sendamail " );
request.setAttribute( " test " , new TestList( 100 , false ) );
%>
< fmt:setLocale value = " <%=session.getAttribute(Globals.LOCALE_KEY)%> " />
< fmt:bundle basename = " ApplicationResources " >
< display:table name = " sessionScope.details " pagesize = " 10 " export = " true " class = " its " decorator = " org.displaytag.sample.Wrapper " >
< display:column property = " link1 " titleKey = " owner.tel " />
< display:column property = " email " titleKey = " owner.email " />
< display:column property = " link2 " title = " Actions " />
< display:column property = " longDescription " media = " csv excel xml pdf " title = " Not On HTML " />
</ display:table >
</ fmt:bundle >
< br >
</ body >
</ html:html >