自己练习写的例子!
struts.xml :
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
- <struts>
- <package name="struts2demo" extends="struts-default">
- <action name="myaction" class="com.MyAction">
- <result name="success">/test.jsp</result>
- </action>
- </package>
- </struts>
Action 页面 MyAction.java:
- package com;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class MyAction extends ActionSupport{
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private String text;
- public String getText() {
- return text;
- }
- public void setText(String text) {
- this.text = text;
- }
- public String execute(){
- List<A> list = new ArrayList<A>();
- list.add(new A("1"));
- list.add(new A("2"));
- list.add(new A("3"));
- list.add(new A("4"));
- list.add(new A("5"));
- HttpServletRequest request = ServletActionContext.getRequest();
- try {
- request.setCharacterEncoding("UTF-8");
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- request.setAttribute("forlist",list);
- request.setAttribute("strChina",ChinesePinyinInitials.getPYString(this.text));
- this.Action(this.text);
- return SUCCESS;
- }
- public void Action(String str){
- System.out.println("******************************************************************");
- System.out.println(ChinesePinyinInitials.getPYString(str));
- }
- }
- class A{
- public String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public A(String name) {
- super();
- this.name = name;
- }
- public A() {
- super();
- }
- }
index.jsp 页面 点击 转换按钮 会跳转到test.jsp :
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <%
- 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>My JSP 'index.jsp' starting page</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>
- <s:form action="myaction.action">
- <s:textfield name="text" label="中文"/>
- <s:submit value="转换"/>
- </s:form>
- </body>
- </html>
test.jsp 页面 循环输出了forlist中的数据 index页面中输入的 中文 转义成了 英文首字母
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- request.setCharacterEncoding("UTF-8");
- Object list=request.getAttribute("forlist");
- Object str=request.getAttribute("strChina");
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>test.jsp</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>
- <s:iterator value="#request.forlist">
- <s:property value="name" /><br/>
- </s:iterator>
- <%=str %>
- </body>
- </html>
源码上传不上来。。上传到51CTO下载中心了 http://down.51cto.com/data/756412