用ActionContext.getContext().getValueStack()获得值栈

value stack是一些对象的几何,最后放到这个集合中的元素是:
Named Objects

These objects include #application, #session, #request, #attr and #parameters and refer to the corresponding servlet scopes

以下代码治理了从表单到后台java,再到前台jsp的中文乱码。

所有jsp页面全用utf-8,然后用getBytes转换。实验了一下我写的乱码过滤监听器,不管用。本文后面源代码中带有这个监听器。

<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Hello World</title>
</head>
<body>
	<h1>Hello World From Struts2</h1>
	<form action="hello">
		<label for="name">请输入中文名字</label><br /> <input type="text" name="name" />
		<input type="submit" value="Say Hello" />
	</form>
</body>
</html>

<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
	Entered value :
	<s:property value="name" />
	<br />
	<%
		String temp = new String();
		temp = (String) request.getAttribute("name");
		temp = new String(temp.getBytes("iso-8859-1"), "utf-8");
		out.println(temp);
		out.println("<br/>");
	%>
	Value of key 1 :
	<s:property value="key1" />
	<br /> Value of key 2 :
	<s:property value="key2" />
	<br />
</body>
</html>

package com.tutorialspoint.struts2;

import java.util.*; 

import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport{
   private String name;

   public String execute() throws Exception {
      ValueStack stack = ActionContext.getContext().getValueStack();
      Map<String, Object> context = new HashMap<String, Object>();

      context.put("key1", new String("这是键1")); 
      context.put("key2", new String("This is key2"));
      stack.push(context);

      System.out.println("Size of the valueStack: " + stack.size());
      return "success";
   }  

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

原文: http://www.tutorialspoint.com/struts_2/struts_value_stack_ognl.htm

源代码:http://pan.baidu.com/share/link?shareid=474582&uk=3878681452


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值