posts - 24,  comments - 25,  trackbacks - 0

Struts2.0属于Web框架,MVC 2模型,其实和以前的Struts1.x没有什么关系,新手可以不用去学。
因为Struts2.0是Webwork2.2演变而来。

第一步:创建Web工程

要使用Struts2.0先要去下载包
http://people.apache.org/builds/struts/2.0.10/struts-2.0.10-lib.zip

打开struts-2.0.10-lib.zip\struts-2.0.10\lib
把里面commons-logging-1.0.4.jar;freemarker-2.3.8.jar;ognl-2.6.11.jar;struts2-core-2.0.11.jar;xwork-2.0.4.jar五个包解压出来,拷贝到你的Web工程的WebContent/WEB-INF/lib下

第二步:配置web.xml文件

<filter>
  
<filter-name>struts</filter-name>
  
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
  
<filter-name>struts</filter-name>
  
<url-pattern>/*</url-pattern>
</filter-mapping>

 

以上配置添加到<web-app></web-app>里
但是我们有时会用到中文,所以我们要重写FilterDispatcher(过滤器)

在src目录下创建com.filter包,在包中建立NewFilter类,继承FilterDispatcher ,代码如下:

package filter;

import org.apache.struts2.dispatcher.FilterDispatcher;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class NewFilter extends FilterDispatcher {

 
private static String encoding = "GB2312";

 
public void init(FilterConfig filterConfig) throws ServletException {
  
super.init(filterConfig);
  String encodingParam 
= filterConfig.getInitParameter("encoding");
  
if (encodingParam != null && encodingParam.trim().length() != 0{
   encoding 
= encodingParam;
  }

 }


 
public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) 
throws IOException, ServletException {
  request.setCharacterEncoding(encoding);
  
super.doFilter(request, response, chain);
 }

}


但是这样改写后发现没效果,其实是web.xml里的配置没有调用而已,所以web.xml的配置要改成:
<filter>
  
<filter-name>struts2</filter-name>
  
<filter-class>com.filter.NewFilter</filter-class>
  
<init-param>
    
<param-name>encoding</param-name>
    
<param-value>GB2312</param-value>
  
</init-param>
</filter>
<filter-mapping>
  
<filter-name>struts2</filter-name>
  
<url-pattern>/*</url-pattern>
</filter-mapping>

不难发现,NewFilter.java里的 private static String encoding = "GB2312";
和web.xml里的<param-name>encoding</param-name><param-value>GB2312</param-value>
其实String encodingParam = filterConfig.getInitParameter("encoding");
就是从web.xml中读出参数名为encoding的值,然后赋给子类中的encoding成员

所以,以后需要改变编码方式只需在web.xml中改 <param-value>的值

第三步:配置struts.xml

在src里新建一个struts.xml文件

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd"
>
<struts>
    
<include file="struts-default.xml"/>
    
<package name="com" extends="struts-default">    
        
<action name="hello" class="com.HelloWorld">
          
<result>Hello.jsp</result>
        
</action>
    
</package>
</struts>

添加到struts.xml里

在src下新建struts.properties添加如下配置:

struts.locale=zh_CN
struts.i18n.encoding=GB2312


这样struts就能识别中文了


第四步:新建JavaBean

在src.com下新建HelloWorld.java,代码如下:
package com;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {
 
private static final long serialVersionUID = 1L;
 
 
private String message;

 
public String getMessage() {
  
return message;
 }


 
public void setMessage(String message) {
  
this.message = message;
 }

 
 
public String execute() {
  System.out.println(
"Executing action, your message is " + message);
  
return SUCCESS;
 }

}



第五步:新建jsp页面

在WebContent下新建HelloWorld.jsp,代码如下:

<%@ page contentType="text/html; charset=GB2312" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    
<head>
        
<title>HelloWorld</title>
        
<s:head />
    
</head>
    
<body>
        
<s:actionerror />
        
<s:form action="hello">
            
<s:textfield label="Name" name="message" tooltip="Enter your Name here" />
            
<s:submit />
        
</s:form>
    
</body>
</html>


在WebContent下新建Hello.jsp,代码如下:

<%@ page contentType="text/html; charset=GB2312" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    
<head>
        
<title>HelloWorld</title>
        
<s:head />
    
</head>
    
<body>
        
<h1><s:property value="message" /></h1>
    
</body>
</html>


以上工作完成可以把web工程打包,发布到tomcat或其他web服务器
在地址栏输入:http://localhost:8080/你的web工程名称/HelloWorld.jsp

在输入框输入信息点击submit提交,会在Hello.jsp上显示出你刚才输入的信息

总结:

上面的例子简单地演示了,Web 应用程序的基本操作。第一,配置并不复杂;第二,action提交方式的改变,直接在struts.xml里配置,而不再像1.x里使用

request.forwardmapping("");

来提交;第三,struts2的标签库用起来更简单。

posted on 2008-01-10 12:02 Jarry 阅读(1166) 评论(0)  编辑  收藏 所属分类: Struts2.x

只有注册用户登录后才能发表评论。


网站导航: