com.action.OwnerAction.java负责处理业务逻辑(代码见下面)
com.actionform.OwnerForm.java负责接受请求(同上)
另外先在这里建立资源文件,即ApplicationResource.properties,ApplicationResource.source,ApplicationResource_en.properties,ApplicationResource_zh_CN.properties
第四步,配置 web.xml与struts-config.xml
web.xml代码如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-template.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.base.AppBaseServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
struts-config.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<!-- ========== Form Bean Definitions ================================== -->
<form-beans>
<form-bean name="ownerForm" type="com.actionform.OwnerForm">
</form-bean>
</form-beans>
<!-- ========== Global Exception Definitions ============================== -->
<global-exceptions />
<!-- ========== Global Forward Definitions =============================== -->
<global-forwards>
<forward name="failure" path="/failure.jsp" />
</global-forwards>
<!-- ========== Action Mapping Definitions =============================== -->
<action-mappings>
<action
input="owner.jsp"
name="ownerForm"
path="/owner"
type="com.action.OwnerAction"
parameter="formAction"><!--因为action类继承DispatchAction类,所以需要设置这个属性-->
<forward name="owner" path="/index.jsp" />
<forward name="success" path="/success.jsp" />
</action>
</action-mappings>
<message-resources parameter="ApplicationResources" />
</struts-config>
红色为关键代码
第五步,建立Owner.jsp文件
<%@ page contentType="text/html;charset=utf-8" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html locale="true">
<head> <meta name = "Generator" content = "Easy Struts Xslt generator for Eclipse (http://easystruts.sf.net)."> <title>Struts Form for ownerForm</title> <script language="javascript"> function changeLanguage(language) { if(language == "en") { document.all("language").value = "en"; } else { document.all("language").value = "cn"; } document.forms[0].submit(); } </script> </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" /> : <html:text property="name"/><html:errors property="name"/></br>
<bean:message key="owner.address" /> : <html:text property="address"/><html:errors property="name"/></br>
<bean:message key="owner.email" /> : <html:text property="email"/><html:errors property="name"/></br>
<bean:message key="owner.tel" /> : <html:text property="tel"/><html:errors property="name"/></br>
<html:submit property="submit" value="Submit"/>
<html:reset value ="Reset"/>
<input type="button" name="langEn" value="英文版" οnclick="changeLanguage('en')">
<input type="button" name="langCn" value="中文版" οnclick="changeLanguage('cn')">
</html:form>
</body>
</html:html>
第六步,建立index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>我的首页</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<br>
<%out.print(session.getAttribute("username")); %>
</body>
</html>
第七步:建立actionform
package com.actionform;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
/**
* OwnerForm.java created by EasyStruts - XsltGen.
* http://easystruts.sf.net
* created on 10-09-2004
*
* XDoclet definition:
* @struts:form name="ownerForm"
*/
public class OwnerForm extends ActionForm {
// --------------------------------------------------------- Instance Variables
/** Email property */
private String email;
/** Greet property */
private String greet = "Mr.";
/** Address property */
private String address;
/** Tel property */
private int tel = 0;
/** Name property */
private String name;
// --------------------------------------------------------- Methods
public String getGreet() {
return greet;
}
public void setGreet(String greet) {
this.greet = greet;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getTel() {
return tel;
}
public void setTel(int tel) {
this.tel = tel;
}
}
第八步:建立action
package com.action;
//Created by Xslt generator for Eclipse.
//XSL : not found (java.io.FileNotFoundException: (Bad file descriptor))
//Default XSL used : easystruts.jar$org.easystruts.xslgen.JavaClass.xsl
import com.actionform.*;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.actionform.*;
/**
* OwnerAction.java created by EasyStruts - XsltGen.
* http://easystruts.sf.net
* created on 10-09-2004
*
* XDoclet definition:
* @struts:action path="/owner" name="ownerForm" input="/owner.jsp" validate="true"
* @struts:action-forward name="/success.jsp" path="/success.jsp"
*/
public class OwnerAction extends Action {
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest servletRequest,
HttpServletResponse servletResponse) {
OwnerForm form = (OwnerForm)actionForm;
HttpSession session=servletRequest.getSession();
session.setAttribute("username",form.getName());
System.out.println(form.getName());
return(actionMapping.findForward("owner"));
}
}
第九步:也是很关键的一步,没有这步你运行也许成功,但都是乱码
cmd中执行native2ascii -encoding gb2312 ApplicationResources.source ApplicationResources_zh_CN.properties。
ApplicationResources.source :代码如下
owner.greet=称呼
owner.name=姓名
owner.address=地址
owner.email=电子邮件
owner.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
你只需在struts-config.xml中做如下配置 <message-resources parameter="ApplicationResources" />
你所在国家的计算机就会主动找到你所在国家的字节码。
OK!希望对初学都有所帮助,因为没人帮助过我,实在。。。