??xml version="1.0" encoding="utf-8" standalone="yes"?>
1.创徏一个服务页面来展示消息
2.创徏一个ActioncL创徏消息
3.创徏一个连接Actioncd面的映?br> 让我们来看看q个ActionQ服务页面和映射的例子。如果你喜欢也可以用IDE来编辑代码?br>
代码如下Q?br>首先Q定义一个服务页面显C数?br><%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h2><s:property value="message" /></h2>
</body>
</html>
其次Q我们需要一个ActioncL创徏消息
package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
public static final String MESSAGE = "Struts is up and running...";
private String message;
public String execute() throws Exception {
setMessage(MESSAGE);
return SUCCESS;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
最后我们需要一个映来l定所有?br>~辑struts.xml文g来添加HelloWorld映射
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"<struts>
<package name="tutorial" extends="struts-default">
<action name="HelloWorld" class="tutorial.HelloWorld">
<result>/HelloWorld.jsp</result>
</action>
</package>
</struts>
然后部v该应用程序,打开http://localhost:8080/tutorial/HelloWorld.action观察出现了什么,你会看见一个标题ؓHello WorldQ的面q有一?#8220;Struts is up and running”的消息?br>
代码是如何工作的Q?br>1.容器接收到HelloWorld.action的请求。根据web.xml的设|,容器会寻找所有被发送到org.apache.struts2.dispatcher.FilterDispatcher的请求,包括所有的*.action的请求。FilterDispatcher是框架的入口炏V?br>2.框架L一个映名为HelloWorld的ActioncR它扑ֈ与HelloWorldcȝ关的映射Qƈ调用Action的executeҎ?br>3.executeҎ讄了消息ƈq回SUCCESS。如果返回的是SUCCESSQ框架会查ActioncL的哪个面被装蝲。框架会告诉容器l制HelloWorld.jsp作ؓh的响应?br>4.当HelloWorld.jsp被调用,<s:property value="message" />标签调用HelloWorld的ActioncȝgetterҎgetMessageQƈmessage的值合q到响应中去?br>5.一个纯_的响应被发送回览器?br>
试Actionsc,代码如下
package tutorizl
import junit.framework.TestCase;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldTest extends TestCase {
public void testHelloWorld() throws Exception {
HelloWorld hello_world = new HelloWorld();
String results = hello_world.execute();
assertTrue("Expected a success results!", ActionSupport.SUCCESS.equals(results));
assertTrue("Expected the default message!",HelloWorld.MESSAGE.equals(hello_world.getMessage()));
}
}
需要记住的?框架使用ActioncL处理HTML表单和其他请求,Actionc返回一个诸如SUCCESS,ERROR,或INPUT名称。根据来自struts.xml的映,一个返回的名称会选择相应的页面,另外的ActionQ或其他web资源Q图象,PDFQ?br>当一个服务端面被绘Ӟ大多情况下会包括由ActioncL供的动态数据。框架会提供一pd的HTML标签创徏面来得显C动态数据更加简单?/p>
使用getҎ?需要修?$TOMCAT/conf/server.xml部v文g,在connectior属性中dURIEncoding="GBK"卛_
使用postҎ??U方法可?/font>
1) 覆写ActionServlet的processҎ,drequest.setEncoding("GBK");
2) d一个过滤器SetCharacterEncodingFilterc?此类可以?TOMCAT\webapps\servlets-examples\WEB-INF\classes\filters扑ֈ,不过方便赯,自己加了点东?源码如下:
public class SetCharacterEncodingFilter implements Filter {
private FilterConfig filterConfig;
public void destroy()
{
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException
{
// TODO Auto-generated method stub
try{
request.setCharacterEncoding("GB2312");
HttpServletResponse res = (HttpServletResponse)response;
res.setHeader("Pragma", "No-cache");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Pragram", "no-cache");
filterChain.doFilter(request, response);
}catch(ServletException e)
{
filterConfig.getServletContext().log(e.getMessage());
}catch(IOException e)
{
filterConfig.getServletContext().log(e.getMessage());
}
}
public void init(FilterConfig filterConfig) throws ServletException
{
// TODO Auto-generated method stub
this.filterConfig = filterConfig;
}
}
在web.xml中部|这个过滤器
<filter>
<filter-name>servfilter</filter-name>
<filter-class>com.yourcompany.struts.SetCharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>servfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
关于国际化问?我曾l请教过老师,不过老师好像不是q个方向?/font>
后来才发现是一个很愚蠢的问?要求是ApplicationResources_zh_CN.properties或ApplicationResources_zh.properties 而我却用了ApplicationResources_cn.properties
切记CN是大写的
native2ascii -encoding gb2312 application_temp.properties application_CN.properties
native2ascii可以?JAVA_HOME/bin下找?/font>