webwork2 中文问题

1webwork.properties文件中,添加:webwork.i18n.encoding = GBK它主要是用来设置WebWork UI标签库的编码,如果不设置它将通过System.getProperty("file.encoding")来获取默认字符编码。
2
velocity.properties文件中,添加:input.encoding=GBKoutput.encoding=GBKdefault.contentType=text/html; charset=GBK它是用来设置.vm页面的编码方式
3
、写一个Filter,将编码设置为GBK。详细请看附件中的SetCharacterEncodingFilter文件和Web.xml的配置。它解决Action数据传递时的编码。
4
、本文下面的源代码修改自\webwork\docs\wikidocs\TutorialLesson03.html并解决中文问题
SetCharacterEncodingFilter.java

  1 package EncodingFilter;
  2
  3  import java.io.IOException;
  4
  5  import javax.servlet.Filter;
  6
  7  import javax.servlet.FilterChain;
  8
  9  import javax.servlet.FilterConfig;
 10
 11  import javax.servlet.ServletException;
 12
 13  import javax.servlet.ServletRequest;
 14
 15  import javax.servlet.ServletResponse;
 16
 17   public   class  SetCharacterEncodingFilter implements Filter  {
 18
 19 protected String encoding = null;
 20
 21 protected FilterConfig filterConfig = null;
 22
 23 protected boolean ignore = true;
 24
 25 public void destroy() {
 26
 27 this.encoding = null;
 28
 29 this.filterConfig = null;
 30
 31 }

 32
 33 /**//**
 34
 35  * Select and set (if specified) the character encoding to be used to
 36
 37  * interpret request parameters for this request.
 38
 39  *
 40
 41  * @param request The servlet request we are processing
 42
 43  * @param result The servlet response we are creating
 44
 45  * @param chain The filter chain we are processing
 46
 47  *
 48
 49  * @exception IOException if an input/output error occurs
 50
 51  * @exception ServletException if a servlet error occurs
 52
 53  */

 54
 55 public void doFilter(ServletRequest request, ServletResponse response,
 56
 57       FilterChain chain)
 58
 59 throws IOException, ServletException {
 60
 61   if (ignore || (request.getCharacterEncoding() == null)) {
 62
 63   String encoding = selectEncoding(request);
 64
 65   if (encoding != null)
 66
 67   request.setCharacterEncoding(encoding);
 68
 69  }

 70
 71  chain.doFilter(request, response);
 72
 73 }

 74
 75 
 76
 77 /**//**
 78
 79  * Place this filter into service.
 80
 81  *
 82
 83  * @param filterConfig The filter configuration object
 84
 85  */

 86
 87 public void init(FilterConfig filterConfig) throws ServletException {
 88
 89  this.filterConfig = filterConfig;
 90
 91  this.encoding = filterConfig.getInitParameter("encoding");
 92
 93  String value = filterConfig.getInitParameter("ignore");
 94
 95  if (value == null)
 96
 97   this.ignore = true;
 98
 99  else if (value.equalsIgnoreCase("true"))
100
101   this.ignore = true;
102
103  else if (value.equalsIgnoreCase("yes"))
104
105   this.ignore = true;
106
107  else
108
109   this.ignore = false;
110
111 }

112
113 protected String selectEncoding(ServletRequest request) {
114
115  return (this.encoding);
116
117 }

118
119 
120
121}

122
123

web.xml
 1 < filter >
 2
 3          < filter-name > Encoding </ filter-name >
 4
 5          < filter-class > EncodingFilter.SetCharacterEncodingFilter </ filter-class >
 6
 7          < init-param >
 8
 9              < param-name > encoding </ param-name >
10
11              < param-value > GBK </ param-value >
12
13          </ init-param >
14
15      </ filter >
16
17      < filter-mapping >
18
19          < filter-name > Encoding </ filter-name >
20
21          < url-pattern > /* </ url-pattern >
22
23      </ filter-mapping >
24
25
xwork.xml :
 1 <! DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN"
 2 "http://www.opensymphony.com/xwork/xwork-1.0.dtd" >
 3
 4 < xwork >
 5   <!--  Include webwork defaults (from WebWork JAR).  -->
 6   < include  file ="webwork-default.xml"   />
 7  
 8   <!--  Configuration for the default package.  -->
 9   < package  name ="default"  extends ="webwork-default" >
10    <!--  Default interceptor stack.  -->  
11    < default-interceptor-ref  name ="defaultStack"   />  
12   
13    <!--  Action: Lesson 03: HelloAction.  -->
14    < action  name ="hello"  class ="lesson03.HelloAction" >
15     < result  name ="error"  type ="dispatcher" > ex02-index.jsp </ result >
16     < result  name ="success"  type ="dispatcher" > ex02-success.jsp </ result >
17    </ action >
18   </ package >
19 </ xwork >
20
21
HelloAction.java :
 1 package lesson03;
 2
 3 import com.opensymphony.xwork.ActionSupport;
 4
 5 public   class  HelloAction extends ActionSupport  {
 6
 7 String person;
 8
 9 public String getPerson() {
10
11  return person;
12
13 }

14
15 public void setPerson(String person) {
16
17  this.person = person;
18
19 }

20
21 public String execute() throws Exception {
22
23  if ((person == null|| (person.length() == 0)) return ERROR;
24
25  else return SUCCESS;
26
27 }

28
29}

30
31
index.jsp
 1 < html >
 2    < head >
 3      < title > test webwork </ title >
 4    </ head >
 5    < body >
 6    < form  action ="hello.action"  method ="post" >
 7      < input  type ="text"  name ="person"  value ="" />
 8      < input  type ="submit"  value ="Submit" />
 9    </ form >
10    </ body >
11 </ html >
12
13
ex02-index.jsp :
 1 < html >
 2 < head >
 3 < title > WebWork Tutorial - Lesson 3 - Example 2 </ title >
 4 </ head >
 5
 6 < body >
 7
 8 < p > What's your name? </ p >
 9
10 < form  action ="hello.action"  method ="post" >
11 < p >< input  type ="text"  name ="person"   />< input  type ="submit"   /></ p >
12 </ form >
13
14 </ body >
15 </ html >
16

ex02-success.jsp:

 1 <% @ taglib uri="webwork" prefix="ww"  %>
 2 < html >
 3 < head >
 4 < title > WebWork Tutorial - Lesson 3 - Example 2 </ title >
 5 </ head >
 6 < body >
 7
 8 Hello,  < ww:property  value ="person"   />
 9
10 </ body >
11 </ html >
12
13