struts2的搭建和简单的例子(采用struts-2.5.2版本)
2016-09-05 16:47
826 查看
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的过滤器
注:由于strus2.5版本比较新,在使用的过程中可能会出现许多的报错。
四、创建IndexAction类。
1.extends(继承) ActionSupport
2.返回的字符串用于结合配置文件进行跳转
五、新建一个struts.xml配置文件。
六、创建index.jsp页面。
七、运行结构。
页面显示:
控制台输出:
当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的过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>ssh_001</display-name> <welcome-file-list> <welcome-file>index.action</welcome-file> </welcome-file-list> <!-- 配置struts2过滤器 --> <filter> <filter-name>struts-prepare</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class> </filter> <filter> <filter-name>struts-execute</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts-prepare</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts-execute</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
注:由于strus2.5版本比较新,在使用的过程中可能会出现许多的报错。
四、创建IndexAction类。
1.extends(继承) ActionSupport
2.返回的字符串用于结合配置文件进行跳转
package ssh_001; import com.opensymphony.xwork2.ActionSupport; public class IndexAction extends ActionSupport { //1.每一个对外的方法,都是返回String类型 //2.返回的字符串,要跟配置文件一一对应,用于跳转 public String execute(){ System.out.println("我是action,被struts调用"); return "success"; } }
五、新建一个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>
七、运行结构。
页面显示:
控制台输出:
相关文章推荐
- struts2入门-第一个例子==》第一个struts2实例——HelloWorld==》Struts2.0搭建和简单实例==>写一个Steuts2的经典入门案例,做了一个登陆程序作为入门例子
- 尚硅谷公开课--struts2--2--搭建struts2环境以及struts2简单例子
- VS2013下编译osip和exosip的5.0版本静态库及搭建和简单例子的实现
- SSH之struts2——搭建struts2,以及简单的登陆功能测试
- Eclipse搭建Struts框架,及一个简单的Struts例子
- Spring Cloud简介/版本选择/ZooKeeper例子搭建简单说明
- Django2.0从搭建环境到部署到服务器简单例子及版本问题
- Struts2 2.3.30版本的HelloWorld简单例子
- 以一个简单的登录的例子来演示如何搭建struts1环境
- Eclipse搭建Struts框架,及一个简单的Struts例子
- JAVA-STRUTS简单例子
- 阿翔编程学-struts2.0简单的例子
- Struts中应用Struts-menu的简单例子
- 一个简单的Struts +ajax 的例子
- struts简单例子的分析
- Eclipse + MyEclipse整合Struts+Spring+Hibernate简单例子开发
- Struts_DispatchAction简单的例子
- struts简单例子的分析(含代码注释和配置文件说明)
- [Struts]学习日记1 - 一个简单的例子
- struts简单例子的分析(含代码注释和配置文件说明)