首页 > 技术文章 > struts2的搭建和简单的例子(采用struts-2.5.2版本)

laibin 2016-09-05 16:47 原文

struts框架的概述:

  当2001年初,Struts的第一个版本在apache网站上发布,它提供了一种分离视图和业务应用逻辑的web应用方案。

  在Struts诞生之前,开发人员都是在jsp里写入处理业务逻辑的Java代码,尤其是涉及到数据库和页面Form表单数据之间交互的时候,开发人员在每个页面都要写入像链接数据库这样的Java代码,导致了大量的代码冗余,而且每个页面显示速度和性能都不是很好,这是因为页面中的存储数据的Java对象都需要从内存中读取,势必影响性能。所有当像Struts这种web应用方案一出现,每个开发人员都把它视为把自己从繁重的开发工作中解放出来的利器,大量的为企业做web应用系统的IT公司在项目架构中都采取struts作为开发中必须使用的框架 

 

一、在eclipse(开发工具)里创建web项目(项目名称:ssh_001),并生成web.xml文件

 

二、导入struts的lib包(struts2-showcase项目中的lib包)

  官方网站下载地址:http://struts.apache.org/download.cgi#struts252

 

  找到这个目录下的struts2-showcase.war包,将war包导入到eclipse中就可以查看项目的内容了

  

  

 查看struts2-showcase下的lib里jar包,将其拷贝出来。

 把spring开头的删除
 把struts2-spring-plugin-2.5.2.jar删除
 把tiles开头的删除 

 注:此版本为struts2.5(jar包是自己struts2-showcase项目中的lib包拷贝的,可能还有一些是没有使用到的,但没关系,不影响运行!)

 

三、在配置文件web.xml配置一个struts2的过滤器

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 4     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 5     id="WebApp_ID" version="3.1">
 6     <display-name>ssh_001</display-name>
 7     <welcome-file-list>
 8         <welcome-file>index.action</welcome-file>
 9     </welcome-file-list>
10     <!-- 配置struts2过滤器 -->
11     <filter>
12         <filter-name>struts-prepare</filter-name>
13         <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
14     </filter>
15 
16     <filter>
17         <filter-name>struts-execute</filter-name>
18         <filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class>
19     </filter>
20 
21     <filter-mapping>
22         <filter-name>struts-prepare</filter-name>
23         <url-pattern>/*</url-pattern>
24     </filter-mapping>
25 
26     <filter-mapping>
27         <filter-name>struts-execute</filter-name>
28         <url-pattern>/*</url-pattern>
29     </filter-mapping>
30 </web-app>

注:由于strus2.5版本比较新,在使用的过程中可能会出现许多的报错。

 

四、创建IndexAction类。

1.extends(继承) ActionSupport
2.返回的字符串用于结合配置文件进行跳转

 1 package ssh_001;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class IndexAction extends ActionSupport {
 6     
 7     //1.每一个对外的方法,都是返回String类型
 8     //2.返回的字符串,要跟配置文件一一对应,用于跳转
 9     public String execute(){
10         System.out.println("我是action,被struts调用");
11         return "success";
12     }
13 
14 }

 

 五、新建一个struts.xml配置文件。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<!-- 上面的头,注意版本,从样例里复制过来
showcase.war\WEB-INF\src\java\struts.xml
 -->
 
<struts>
    <!-- 第1步:先定义一个包 -->
    <package name="mypck" extends="struts-default">
        <!-- 第2步:定义一个action,配置跳转信息 
        name 类似于Servlet @WebServlet("/IndexServlet")
        class 对应于自己写的Action类
        当不写method属性时,默认调用的是execute
        http://xxxx/xxx/Index.action
        -->
        <action name="Index" class="ssh_001.IndexAction">
            <!-- 配置不同字符串,跳转到不同的页面
                 当Action中的execute方法,返回字符串success,就跳转到index.jsp
             -->
            <result name="success">index.jsp</result>
        </action>
    </package>
</struts>

 

六、创建index.jsp页面。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    第一个struts2框架搭建成功!
</body>
</html>

 

七、运行结构。

页面显示:

控制台输出:

 

推荐阅读