Struts2框架的国际化

本文介绍了一个使用Struts2框架实现的简单应用案例,包括员工信息的封装、国际化设置、登录功能及页面布局。通过配置struts.xml和web.xml,实现了中英文界面切换和用户登录验证。

加粗样式

效果图英文

中文

1.Employee.java 封装

package com.oumyye.action;
import com.opensymphony.xwork2.ActionSupport;

public class Employee extends ActionSupport{
   private String name;
   private String password;
     
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public String getpassword() {
       return password;
   }
   public void setpassword(String password) {
       this.password = password;
   }
   public String execute() {
       return SUCCESS;
   }
}
2.Locale.java  文件
     接受数据

package com.oumyye.action;

import com.opensymphony.xwork2.ActionSupport;

public class Locale extends ActionSupport{
   public String execute() 
   {
       return SUCCESS;
   }
}

3 中英文的转换

英文
中文

4 struts.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
   <constant name="struts.devMode" value="true" />
   <!-- 扫描资源目录下以global开头的文件 -->
   <constant name="struts.custom.i18n.resources" value="global" />
       <!-- 常量配置资源文件命名-->
   <package name="helloworld" extends="struts-default" namespace="/">
      <action name="empinfo" 
         class="com.oumyye.action.Employee"
         method="execute">
         <result name="input">/index.jsp</result>
         <result name="success">/success.jsp</result>
      </action>
      
      <action name="locale" 
         class="com.oumyye.action.Locale"
         method="execute">
         <result name="success">/index.jsp</result>
      </action>
   </package>

</struts>

5.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>   
 <filter>
      <filter-name>struts2</filter-name>
       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
     <filter-mapping>
         <filter-name>struts2</filter-name>
         <url-pattern>/*</url-pattern>
   </filter-mapping>
  
</web-app>

  1. index.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
   pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>

<body >
  <h1><s:text name="global.heading"/></h1>

  
   <s:url id="indexZH" namespace="/" action="locale" >
      <s:param name="request_locale" >zh_CN</s:param>
   </s:url>
   <s:url id="indexFR" namespace="/" action="locale" >
      <s:param name="request_locale" >en_US</s:param>
   </s:url>

		<!-- href ="locale?request_locale=zh" -->

   <button><s:a href="%{indexZH}" >中文</s:a></button>
   <button><s:a href="%{indexFR}" >English</s:a></button>

   <s:form action="empinfo" method="post" namespace="/">
      <s:textfield name="name" key="global.name" size="20" />
      <s:textfield name="password" key="global.password" size="20" />
      <s:submit name="submit" key="global.submit" />
   </s:form>

</body>
</html>
     页面的布置

7.scuccess.jsp文件

  主要是接受登录成功的数据

<%@ page language="java" contentType="text/html; charset=UTF-8"
   pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Success</title>
</head>
<body>
恭喜你,提交成功!
   
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值