当前位置: 首页 > ds >正文

struts2教程--快速入门

Struts2框架介绍

1、三大框架 : 是企业主流 JavaEE开发的一套架构

Struts2 + Spring + Hibernate

2、 什么是框架?为什么要学框架 ?

框架 是 实现部分功能的代码 (半成品),使用框架简化企业级软件开发

学习框架 ,清楚的知道框架能做什么?还有哪些工作需要自己编码实现 ?

3、 什么是Struts2?

Struts2 是一款优秀MVC框架

MVC:是一种思想,是一种模式,将软件分为 Model模型、View视图、Controller控制器

 * MVC由来是web开发

JavaEE软件三层结构 : web层(表现层)、业务逻辑层、数据持久层 (sun提供JavaEE开发规范)

JavaEE开发更强调三层结构, web层开发注重MVC

struts2 就是 web层开发框架,符合MVC模式

 * struts1 、webwork、jsf、SpringMVC都是MVC

4、 Struts2和 Struts1关系

没有关系, Struts2 全新框架,引入WebWork很多技术和思想,Struts2保留Struts1类似开发流程

 * Struts2 内核 webwork  

Xwork提供了很多核心功能:前端拦截机(interceptor),运行时表单属性验证,类型转换,强大的表达式语言(OGNL– the Object Graph Navigation Language),IoC(Inversion of Control反转控制)容器等

下面这是struts的一张架构图

这些大概就是struts的核心的东西了。

Struts2快速入门

1、 下载开发包

这个在官网下载就可以了。

2、 目录结构

apps : struts2官方demo  

docs :  文档

lib : jar包

src : 源码

3、 导入jar包到开发工程

只需要导入 apps/struts2-blank.war 中所有jar包  ---- 13个jar包

4、 编写页面

hello.jsp 请求页面

<a href="${pageContext.request.contextPath }/hello.action">访问struts2入门</a>

success.jsp 结果页面

5、在web.xml配置struts2前端控制器 (Filter)

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

  

6、执行struts2过滤器后,读取struts2配置文件,将请求分发

在src下创建struts.xml

  <package name="default" namespace="/" extends="struts-default"><!-- <a href="${pageContext.request.contextPath }/hello.action">访问struts2入门</a> --><!-- 将请求 分发给一个Action --><!-- action的name 就是hello.action 去掉扩展名  --><action name="hello" class="com.sihai.struts2.demo1.HelloAction"></action></package>

拦截器实现:

package com.sihai.filter;import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class StrutsFilter implements Filter {public void init(FilterConfig filterConfig) throws ServletException {}public void doFilter(ServletRequest req, ServletResponse resp,FilterChain chain) throws IOException, ServletException {// 1.强转HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) resp;// 2.操作// 2.1 得到请求资源路径String uri = request.getRequestURI();String contextPath = request.getContextPath();String path = uri.substring(contextPath.length() + 1);// System.out.println(path); // hello// 2.2 使用path去struts.xml文件中查找某一个<action name=path>这个标签SAXReader reader = new SAXReader();try {// 得到struts.xml文件的document对象。Document document = reader.read(new File(this.getClass().getResource("/struts.xml").getPath()));Element actionElement = (Element) document.selectSingleNode("//action[@name='" + path + "']"); // 查找<action// name='hello'>这样的标签if (actionElement != null) {// 得到<action>标签上的class属性以及method属性String className = actionElement.attributeValue("class"); // 得到了action类的名称String methodName = actionElement.attributeValue("method");// 得到action类中的方法名称。// 2.3通过反射,得到Class对象,得到Method对象Class actionClass = Class.forName(className);Method method = actionClass.getDeclaredMethod(methodName);// 2.4 让method执行.String returnValue = (String) method.invoke(actionClass.newInstance()); // 是让action类中的方法执行,并获取方法的返回值。// 2.5// 使用returnValue去action下查找其子元素result的name属性值,与returnValue做对比。Element resultElement = actionElement.element("result");String nameValue = resultElement.attributeValue("name");if (returnValue.equals(nameValue)) {// 2.6得到了要跳转的路径。String skipPath = resultElement.getText();// System.out.println(skipPath);request.getRequestDispatcher(skipPath).forward(request,response);return;}}} catch (DocumentException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (NoSuchMethodException e) {e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InstantiationException e) {// TODO Auto-generated catch blocke.printStackTrace();}// 3.放行chain.doFilter(request, response);}public void destroy() {}}

7、执行目标Action中execute方法 

8、在Action的execute方法中返回 字符串,在struts.xml中配置字符串与页面对应关系

<result name="executesuccess">/demo1/success.jsp</result>  完成结果页面跳转

下面是具体的源代码

helloAction.java:

package com.sihai.action;public class HelloAction {public String say() {System.out.println("hello action say method");return "good";}
}
com.sihai.action;public class HelloAction {public String say() {System.out.println("hello action say method");return "good";}
}

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><package name="default" namespace="/" extends="struts-default"><action name="hello" class="com.sihai.action.HelloAction"method="say"><result name="good">/hello.jsp</result></action></package><package name="dd" namespace="/abc" extends="struts-default"><action name="hello" class="com.sihai.action.HelloAction"method="say"><result name="good">/hello.jsp</result></action></package>
</struts>
com.sihai.action.HelloAction"method="say"><result name="good">/hello.jsp</result></action></package><package name="dd" namespace="/abc" extends="struts-default"><action name="hello" class="com.sihai.action.HelloAction"method="say"><result name="good">/hello.jsp</result></action></package>
</struts>

Struts2流程分析与工具配置

1、 运行流程

请求 ---- StrutsPrepareAndExecuteFilter核心控制器 ----- Interceptors拦截器(实现代码功能 ) ----- Action的execuute ---结果页面 Result

* 拦截器 在 struts-default.xml定义

* 执行拦截器 是 defaultStack中引用拦截器

2、 配置struts.xml提示问题

 如果安装Aptana编辑器 ,请不要用Aptana自带xml编辑器 编写struts2配置文件

 struts.xml提示来自于 DTD约束,

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

如果可以上网,自动缓存dtd,提供提示功能

如果不能上网,也可以配置本地DTD提示

3、 关联struts2源码

关联 zip包

4、 Config Brower插件使用

提供在浏览器中查看 struts2 配置加载情况

将解压struts2/lib/struts2-config-browser-plugin-2.3.7.jar复制WEB-INF/lib下

访问 http://localhost:8080/struts2_day1/config-browser/index.action查看 struts2配置加载情况

 

http://www.xdnf.cn/news/11923.html

相关文章:

  • shutdown 命令参数介绍
  • HTML5+CSS3案例一:学成在线
  • 人工智能5:构建基于iris 数据集的 SVM 分类模型,含有 iris.csv
  • DAVINCInbsp;DM365-DM368开发攻略…
  • 【2024最新版】超详细Wireshark安装保姆级教程,及简单使用Wireshark抓包_wireshark官网
  • Dopamine(多巴胺)越狱工具一键越狱教程:支持 iOS 15-iOS 16.6.1 设备
  • 完整图书馆管理系统(包含设计思路、图形界面、后台数据库)
  • 百度二级域名大全 目前为234个http://www.twocity.cn/blog/article.asp?id=818
  • 转vmp3.0.9全保护拆分解析
  • htc G10刷机教程
  • VC 界面库皮肤库
  • 植物大战僵尸:逆向分析阳光
  • 手把手教你搭建自己的个人博客(图文教程)
  • 【零基础从入门到精通】程序、C语言
  • 千渡互通 (1000du2) V1.0 客户端 http://down.hotlife.cn/html/download/2006/6/06/1149565794.shtml
  • ASP网站漏洞解析及黑客入侵防范方法
  • 在linux用utorrent下载文件,在Ubuntu 10.04下使用uTorrent下载六维空间资源
  • 中国最好的博客网站
  • 防守地图隐藏英雄密码全集
  • Android开发——Dialog对话框
  • Hack games
  • 免费电子书籍下载站点大全
  • 获取myeclipse 7.5 注册码的方法
  • 什么是站内搜索引擎?如何在网站中加入站内搜索功能?
  • Microsoft Office2003sp2_5in1 迷你第7版(最终完美版)
  • gridview删除
  • Android下修改hosts文件
  • Red5的使用--使用日志
  • php抽奖算法
  • 推荐几个好的粉碎文件的软件?这3款软件让你彻底摆脱无法删除文件的烦恼