关于Tomcat + struts的环境我就不再叙述了,可以参考超简单配置tomcat + struts 。 建议看着篇文章之前先看看我另一片文章《手把手struts入门示例》 ,在本文,我不再作解析。
Helloword! 咱也俗一下,先来say 一下 hello。
package
hello;
import
org.apache.struts.action.ActionForm;
public
class
helloActionForm
extends
ActionForm
...
{ private String yourName; public void setYourName(String yourName) ... { this .yourName = yourName; } public String getYourName() ... { return yourName; } }
helloActionForm.java
package
hello;
import
org.apache.struts.action.
*
;
import
javax.servlet.http.
*
;
public
class
helloAction
extends
Action
...
{ public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) ... { helloActionForm actionForm = (helloActionForm)form; String yName = actionForm.getYourName() ; yName = yName + " A " ; actionForm.setYourName(yName); return mapping.findForward( " showName " ); } }
helloAction.java
<%
...
@ taglib uri = " http://struts.apache.org/tags-html " prefix = " html "
%>
<
BR
>
Enter Your Name:
<
html:form
action
="/sayhi"
focus
="yourName"
>
<
html:text
property
="yourName"
/>
<
html:submit
property
="submit"
value
="SayHello"
/>
</
html:form
>
index.jsp
<%
...
@ taglib uri = " http://struts.apache.org/tags-bean " prefix = " bean "
%>
Your Name is
<
bean:write
name
="helloForm"
property
="yourName"
scope
="request"
/>
<
jsp:include
page
="index.jsp"
flush
="true"
/>
result.jsp
编译:
javac -classpath "D:/.../apache-tomcat-5.5.12/common/lib/servlet-api.jar";"D:/.../struts-1.3.8/lib/struts.jar";. *.java
<?
xml version
=
"
1.0
"
encoding
=
"
ISO-8859-1
"
?>
<!
DOCTYPE struts
-
config PUBLIC
"
-//Apache Software Foundation//DTD Struts Configuration 1.1//EN
"
"
http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd
"
>
<
struts
-
config
>
<!--
========================================
Form Bean Definitions
-->
<
form
-
beans
>
<
form
-
bean name
=
"
helloForm
"
type
=
"
hello.helloActionForm
"
/>
</
form
-
beans
>
<!--
===================================
Action Mapping Definitions
-->
<
action
-
mappings
>
<
action path
=
"
/sayhi
"
type
=
"
hello.helloAction
"
name
=
"
helloForm
"
scope
=
"
request
"
validate
=
"
false
"
>
<
forward name
=
"
showName
"
path
=
"
/result.jsp
"
/>
</
action
>
</
action
-
mappings
>
</
struts
-
config
>
struts-config.xml 配置到空的struts 项目中 struts-blank-1.*,并改名为hello.目录结构如下。hello │ │ index.jsp │ result.jsp │ ├─META-INF │ │ MANIFEST.MF │ │ │ └─maven │ └─org.apache.struts │ └─struts-blank │ pom.properties │ pom.xml │ ├─pages │ Welcome.jsp │ └─WEB-INF │ struts-config.xml │ validation.xml │ web.xml │ ├─classes │ │ MessageResources.properties │ │ │ └─hello │ helloAction.class │ helloActionForm.class │ ├─lib │ antlr-2.7.2.jar │ commons-beanutils-1.7.0.jar │ commons-chain-1.1.jar │ commons-digester-1.8.jar │ commons-logging-1.0.4.jar │ commons-validator-1.3.1.jar │ oro-2.0.8.jar │ struts-core-1.3.8.jar │ struts-taglib-1.3.8.jar │ struts-tiles-1.3.8.jar │ └─src │ build.xml │ README.txt │ └─java MessageResources.properties 然后把整个hello文件夹放到Tomcat的webapps下,访问http://localhost:8080/hello
OK!到现在为止,我们对struts的基本知识复习了一下。我们继续深入一点。换一种方式来访问类里面的属性。我们增加一个helloNameList.java
package
hello;
import
java.util.List;
import
java.util.Arrays;
import
java.lang.reflect.Array;
public
class
helloNameList
...
{ public String className; public String[] student; public helloNameList() ... { student = new String[] ... { " Tom " , " John " , " Lucy " , " Lily " } ; className = " 二年三組 " ; } public void setClassName(String cname) ... { this .className = cname; } public String getClassName() ... { return className ;} public String[] getStudent() ... { return student; } public String getAstudent( int index) ... { return student[index];} public int getRank(String studentName) ... { if (studentName.equals( " Tom " )) return 1 ; else if (studentName.equals( " John " )) return 2 ; else if (studentName.equals( " Lucy " )) return 3 ; else if (studentName.equals( " Lily " )) return 4 ; else return 0 ; } }
然后扩充一下我们的index.jsp
<%
...
@ taglib uri = " http://struts.apache.org/tags-html " prefix = " html "
%>
<%
...
@ taglib uri = " http://struts.apache.org/tags-bean " prefix = " bean "
%>
<
BR
>
Enter Your Name:
<
html:form
action
="/sayhi"
focus
="yourName"
>
<
html:text
property
="yourName"
/>
<
html:submit
property
="submit"
value
="go"
/>
</
html:form
>
<
br
>
<
jsp:useBean
id
="class1"
class
="hello.helloNameList"
/>
<!--
先引用bean
-->
<
bean:define
id
="myClass"
name
="class1"
property
="className"
/>
<!--
再定义
-->
<
bean:define
id
="myName"
name
="class1"
property
="student[1]"
/>
私は
<%
=
myClass
%>
の
<%
=
myName
%>
です。
<
br
>
<
bean:define
id
="myClassmate"
name
= "class1"
property
= "astudent[2]"
/>
<!--
注意不是astudent(2)
-->
<
bean:define
id
="ranking"
name
= "class1"
property
= "rank(Lucy)"
/>
<!--
字符型参数的,注意与上面的区别
-->
<%
=
myClassmate
%>
is my classmate. Rank is
<%
=
ranking
%>
<
br
>
<!--
我们还可以定义与类无关的数据
-->
<
bean:define
id
="myString"
value
="Hello"
/>
<
bean:define
id
="firstChar"
name
="myString"
property
= "bytes[0]"
/>
<!--
相当于调用getBytes()方法,返回byte数组的第一数据
-->
<%
=
firstChar
%>
<
br
>
<
jsp:setProperty
name
="class1"
property
="className"
value
="Grade 2 Class 3"
/>
<
bean:define
id
="myClass"
name
= "class1"
property
="className"
/><
br
>
My Class is
<%
=
myClass
%>
编译,配置,然后访问http://localhost:8080/hello 是不是看见乱码了。我们在index.jsp开头添加下面语句 <%@ page contentType="text/html; charset=Shift_JIS" %> 可是从bean来的数据还是乱码,提交表达后,都是乱码了。虽然通过右键➔编码➔日语(自动选择)部分正常显示,可是从bean来的数据还是乱码。那我们给result.jsp开头也添加同样的语句。这样子,提交表单后,来自bean的数据正常显示了,可是无乱怎么弄总有一方乱码的吧。为了解决这个问题,我们需要一位朋友帮忙--Filter。现在我们来介绍一下这位新朋友。Filter filter是struts2开始有的。当你想在收到request之前,发出response之前,想干点什么的时候,就需要它帮满了。 读书不求甚解,实践出真知。我们先用上再慢慢解说。使用filter,我们要实现javax.servlet.Filter接口,重写其中的3个方法:init,doFilter和destroy() SetCharacterEncodingFilter.java
package
filters;
import
java.io.IOException;
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
import
javax.servlet.UnavailableException;
public
class
FilterEncoding
implements
Filter
...
{ private FilterConfig filterConfig = null ; private String encoding = null ; public void init(FilterConfig filterConfig) throws ServletException ... { this .filterConfig = filterConfig; this .encoding = filterConfig.getInitParameter( " encoding " ); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException ... { String encoding = this .encoding; if (encoding != null ) ... { request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); } chain.doFilter(request, response); } public void destroy() ... { this .encoding = null ; this .filterConfig = null ; } }
把编译好的类文件放到WEB-INF/classes/filters下, 我们还需要配置一下web.xml,在<web-app>之间加上
<
filter
>
<
filter-name
>
setEncoding
</
filter-name
>
<
filter-class
>
filters.FilterEncoding
</
filter-class
>
<
init-param
>
<
param-name
>
encoding
</
param-name
>
<
param-value
>
Shift_JIS
</
param-value
>
</
init-param
>
</
filter
>
<
filter-mapping
>
<
filter-name
>
setEncoding
</
filter-name
>
<
url-pattern
>
/*
</
url-pattern
>
</
filter-mapping
>
貌视修正了。这回bean的文字正常显示了,但是网页上写的文字,怎么也无法正常显示了。怎么回事呢。在这里又作为一道作业留给大家。 一般乱码问题可以尝试从以下方面解决。 ①jsp文件本身的编码问题。 ②EncodingFilter 的设定(web.xml) ③Tomcat5 以上GET出现乱码的时候,在server.xml的Connector tag里面追加 <Connector ... useBodyEncodingForURI="true"/> ④使用Tomcat,request出现乱码的时候,在server.xml的Connector tag里面追加,编码根据encodingfilter的设定<Connector ... URIEncoding="UTF-8"/>
还可以参考这篇文章"struts中文问题","struts国际化问题"的终极解决方案