Struts2边学边练(1)-HelloWorld

Struts2边学边练(1)-HelloWorld
从今天开始,我将陆续发布自己学习Struts2中的感受和笔记。这也算是督促自己学些和总结学习经验的好方式。

声明一下,我博客中所有的文章仅供本人学习之用,最近有人对我的文章评论过激,在此我希望您不要耽搁时间在我的博客中。如果文章中有什么不对之处,我欢迎大家指出,但是我希望您珍惜自己的言行。

开发环境:

Web服务器:apache-tomcat-6.0.18

Struts版本:struts-2.0.14

JDK版本:JDK1.5.0_12

Eclipse版本:eclipse-jee-ganymede-SR1-win32 也就是eclipse的开发JEE版本,很多人都使用myeclipse,但是由于myeclipse是商业版本,所以觉得eclipse-jee-ganymede对于开发JEE的项目已经很不错了,所以我觉得没必要用myEclipse去开发。

Struts2需要的jar包:

至少需要如下五个包

struts2-core-2.0.11.1.jar

xwork-2.0.4.jar

commons-logging-1.0.4.jar

freemarker-2.3.8.jar

ognl-2.6.11.jar

在这个简单的例子中,我们将会完成以下步骤:
1.配置web.xml
2.编写jsp
3.编写Action实现类
4.配置Action
5.发布运行

1.配置web.xml
Struts2的入口点是一个Filter,需要将这个入口点配置到web.xml:

<? xml version="1.0" encoding="UTF-8" ?>
< web-app  id ="WebApp_ID"  version ="2.4"  xmlns ="http://java.sun.com/xml/ns/j2ee"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
    
    
< display-name > HelloWorld </ display-name >
    
    
< welcome-file-list >
        
< welcome-file > index.jsp </ welcome-file >
    
</ welcome-file-list >
    
    
< filter >
        
< filter-name > struts2 </ filter-name >
        
< filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class >
    
</ filter >
    
< filter-mapping >
        
< filter-name > struts2 </ filter-name >
        
< url-pattern > /* </ url-pattern >
    
</ filter-mapping >
    
</ web-app >


2. 编写jsp
在这个例子中需要两个jsp,一个是index.jsp, 用于输入用户的名字。第二个jsp是welcome.jsp,用于向用户问候。

在Struts2中只需要一个标签库/struts-tags。这里面包含了所有的Struts2标签。但使用Struts2的标签大家要注意一下。在<s::form>中最好都使用Struts2标签,尽量不要用HTML或普通文本.

index.jsp如下:

<% @ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<% @ 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=ISO-8859-1" >
< title > HelloWorld </ title >
</ head >
< body >
  
< s:form  action ="Hello" >
     
< s:textfield  name ="name"  label ="Please Input Your Name:" ></ s:textfield >
     
< s:submit  value ="Hello" ></ s:submit >
  
</ s:form >
</ body >
</ html >

welcome.jsp如下:

<% @ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<% @ 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=ISO-8859-1" >
< title > HelloWorld </ title >
</ head >
< body >
  Hello 
< s:property  value ="name" />
</ body >
</ html >


3.编写Action类
Struts2.x的Action需要从com.opensymphony.xwork2.ActionSupport类继承。而且Action中已经包含了Struts1中的ActionForm类信息,所以不需要再写ActionForm类。

在这个例子中只编写了一个HelloWorld.java类:

package  com.struts2.action;

import  com.opensymphony.xwork2.ActionSupport;


public   class  HelloWorld  extends  ActionSupport  {

    
private static final long serialVersionUID = -2567455771246284511L;
    
    
private String name;
    
    
    
public String getName() {
        
return name;
    }


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



    
public String execute() throws Exception {
        
        setName(getName());        
        
return SUCCESS;
    }


}


4.配置Action类:
struts2.x中的配置文件一般为struts.xml,放到WEB-INF"classes目录中。下面是在struts.xml中配置动作类的代码:

<? xml version="1.0" encoding="UTF-8"  ?>
<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>

< struts >
    
< package  name ="default"  extends ="struts-default" >
        
< action  name ="Hello"  class ="com.struts2.action.HelloWorld" >
            
< result  name ="success" > /welcome.jsp </ result >
        
</ action >
    
</ package >

</ struts >


5.发布:

在eclipse-jee-ganymede下配置tomcat服务器,很简单。
Windows->Performance->Server->Runntime Environment->Add.随着向导就可以增加tomcat服务器了。
然后鼠标右键点击项目的根目录,选择菜单的Run As->Run on Server就可以发布启动你的项目了。而且还有eclipse自带的浏览器,感觉很不错咯。








你可能感兴趣的:(Struts2边学边练(1)-HelloWorld)