第一个Struts 2程序 !

(1)找到开发Struts2应用需要使用的jar文件。

  下载Struts2-->解压-->在lib文件夹下找到以下开发Struts2程序最少需要的JAR。

  Struts2-core-2.x.x.jar:Struts2框架的核心库。

  XWork-2.x.x.jar:XWork类库,Struts2在其上构建。

  ognl-2.6.x.jar:对象图导航语言,(Object Graph Navigation Language),Struts2框架通过其读写对象的属性。

  freemarker-2.3.x.jar:Struts2的UI标签的模板使用FreeMarker编写。

  commons-logging-1.1.x.jar:ASF出品的日志包,Struts2框架使用这个日志包来支持Log4J和JDK1.4+的日志记录。

  commons-fileupload-1.2.1.jar:文件上传组件,2.16版本后必须加入此文件。

(2)编写Struts2配置文件。

  在Struts2中,Struts2框架通过Filter启动。

  在web.xml中加入以下代码:

 
  
< display - name > Struts Blank </ display - name >

< filter >
< filter - name > struts2 </ filter - name >
< filter - class > org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </ filter - class >
      <!--自从Struts2.1.3以后FilterDispatcher已经标注为过时
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> -->
</ filter >

< filter - mapping >
< filter - name > struts2 </ filter - name >
< url - pattern > /* </url-pattern>
</filter-mapping>

  在StrutsPrepareAndExecuteFilter的init()方法中将会读取类路径下默认的配置文件Struts.xml完成初始化操作。

(3)在Web.xml中加入Struts2MVC框架启动配置。

  在包文件src下加入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 >

</ struts >

  如此Struts2的开发环境已经配置成功,编译没有错误,就开始编写第一个Struts2程序。

  首先编写类文件:helloStruts.java

  在scr下新建一个包:cn.action/helloStruts.java

 
  
package cn.action;

public class Users {
private String message;

public String getMessage() {
return message;
}

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

public String execute(){
message
= " hello World " ;
return " success " ;
}
}


再编写jsp文件hello.jsp

将其存放在WEB-INF目录下。

 
  
<% @ page language = " java " import = " java.util.* " pageEncoding = " ISO-8859-1 " %>


<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
< head >

< title > hello </ title >

< meta http - equiv = " pragma " content = " no-cache " >
< meta http - equiv = " cache-control " content = " no-cache " >
< meta http - equiv = " expires " content = " 0 " >
<!--
< link rel = " stylesheet " type = " text/css " href = " styles.css " >
-->

</ head >

< body >
${message}
</ body >
</ html >

这样就可以配置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 = " hello " namespace = " /test " extends = " struts-default " >
< action name = " index " class = " cn.action.helloStruts " method = " execute " >
< result name = " success " >/ WEB - INF / hello.jsp </ result >
</ action >
</ package >
</ struts >

这样,第一个struts2程序就已经写好了。

编译启动,在浏览器中输入:http://localhost:8080/Struts2/test/index.action

即可在页面上看到  hello World 

发布了26 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/liaoze2010/article/details/70239826