自己练习写的例子!

struts.xml :

 

 
  
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4. <package name="struts2demo" extends="struts-default">  
  5.    <action name="myaction" class="com.MyAction">  
  6.      <result name="success">/test.jsp</result>  
  7.    </action>  
  8.  </package>    
  9. </struts>      

Action 页面 MyAction.java:

 

 
  
  1. package com;  
  2. import java.io.UnsupportedEncodingException;  
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.  
  6. import javax.servlet.http.HttpServletRequest;  
  7.  
  8. import org.apache.struts2.ServletActionContext;  
  9.  
  10. import com.opensymphony.xwork2.ActionSupport;  
  11.  
  12. public class MyAction extends ActionSupport{  
  13.  
  14.     /**  
  15.      *   
  16.      */ 
  17.     private static final long serialVersionUID = 1L;  
  18.       
  19.     private String text;  
  20.  
  21.     public String getText() {  
  22.         return text;  
  23.     }  
  24.  
  25.     public void setText(String text) {  
  26.         this.text = text;  
  27.     }  
  28.  
  29.     public String execute(){  
  30.         List<A> list = new ArrayList<A>();  
  31.         list.add(new A("1"));  
  32.         list.add(new A("2"));  
  33.         list.add(new A("3"));  
  34.         list.add(new A("4"));  
  35.         list.add(new A("5"));  
  36.         HttpServletRequest request = ServletActionContext.getRequest();  
  37.           
  38.         try {  
  39.             request.setCharacterEncoding("UTF-8");  
  40.         } catch (UnsupportedEncodingException e) {  
  41.             // TODO Auto-generated catch block  
  42.             e.printStackTrace();  
  43.         }  
  44.         request.setAttribute("forlist",list);  
  45.         request.setAttribute("strChina",ChinesePinyinInitials.getPYString(this.text));  
  46.         this.Action(this.text);  
  47.         return SUCCESS;  
  48.     }  
  49.       
  50.     public void Action(String str){  
  51.         System.out.println("******************************************************************");  
  52.         System.out.println(ChinesePinyinInitials.getPYString(str));  
  53.     }  
  54. }  
  55.  
  56. class A{  
  57.     public String name;  
  58.  
  59.     public String getName() {  
  60.         return name;  
  61.     }  
  62.  
  63.     public void setName(String name) {  
  64.         this.name = name;  
  65.     }  
  66.       
  67.     public A(String name) {  
  68.         super();  
  69.         this.name = name;  
  70.     }  
  71.  
  72.     public A() {  
  73.         super();  
  74.     }  
  75.       
  76. }  

index.jsp 页面 点击 转换按钮 会跳转到test.jsp :

 

 
  
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags"%>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.  
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>My JSP 'index.jsp' starting page</title>  
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!--  
  20.     <link rel="stylesheet" type="text/css" href="styles.css">  
  21.     -->  
  22.   </head>  
  23.     
  24.   <body>  
  25.     <s:form action="myaction.action">  
  26.         <s:textfield name="text" label="中文"/>  
  27.         <s:submit value="转换"/>  
  28.     </s:form>   
  29.   </body>  
  30. </html>  

test.jsp 页面 循环输出了forlist中的数据 index页面中输入的 中文 转义成了 英文首字母

 

 
  
  1. <%@ page language="java" pageEncoding="utf-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags"%>  
  3. <%  
  4.  
  5. String path = request.getContextPath();  
  6.  
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  8. request.setCharacterEncoding("UTF-8");  
  9. Object list=request.getAttribute("forlist");  
  10. Object str=request.getAttribute("strChina");  
  11.  
  12. %>  
  13.  
  14.  
  15. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  16. <html>  
  17.   <head>  
  18.       
  19.     <title>test.jsp</title>  
  20.  
  21.     <meta http-equiv="pragma" content="no-cache">  
  22.     <meta http-equiv="cache-control" content="no-cache">  
  23.     <meta http-equiv="expires" content="0">      
  24.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  25.     <meta http-equiv="description" content="This is my page">  
  26.     <!--  
  27.     <link rel="stylesheet" type="text/css" href="styles.css">  
  28.     -->  
  29.       
  30.   </head>  
  31.     
  32.   <body>  
  33.      <s:iterator value="#request.forlist">  
  34.         <s:property value="name" /><br/>  
  35.     </s:iterator>  
  36.     <%=str %>  
  37.   </body>  
  38. </html>  

 源码上传不上来。。上传到51CTO下载中心了 http://down.51cto.com/data/756412