复制代码
1
2
Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。Struts 2以WebWork为 核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与 ServletAPI完全脱离开,所以Struts 2可以理解为WebWork的更新产品。
Struts2的基本流程如下:
① Web浏览器收到一个请求(HttpServletRequest)。
② 过滤器Dispatcher查找请求,确定适当的Action。
③ 拦截器自动对请求应用通用功能,如验证和文件上传等操作。
④ Action的execute方法通常用来存储和(或)重新获得信息(通过数据库)。
⑤ 结果被返回到浏览器,可能是HTML、图片、PDF或其他。
下面以一个简单的实例来说说struts2。
首先将strut2所需的jar包导入
接着修改web.xml文件,Struts1的入口点是一个Servlet,而Struts2的入口点是一个过滤器(Filter)。因此,Struts2要按过滤器的方式配置
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/JavaEE/web-app_2_5.xsd"> <filter> <filter-name>struts 2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts 2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
创建hello.jsp
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13<%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <title>struts 2应用</title> </head> <body> <form action="struts.action" method="post"> 请输入姓名:<input type="text" name="name"/><br> <input type="submit" value="提交"/> </form> </body> </html>
然后实现Action类,该类需要从com.opensymphony.xwork2.ActionSupport类继承
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package org.action; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class StrutsAction extends ActionSupport{ private String name; public String getName() { return name; } public void setName(String name) { this.name=name; } public String execute() throws Exception { if(!name.equals("HelloWorld")){ Map request=(Map)ActionContext.getContext().get("request"); request.put("name",getName()); return "success"; }else{ return "error"; } } }
创建struts.xml文件,就是配置Action类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13<?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="struts" class="org.action.StrutsAction"> <result name="success">/welcome.jsp</result> <result name="error">/hello.jsp</result> </action> </package> </struts>
其中<package>标签中name标签是指定包名,这个名字将作为引用该包的键。注意,包的名字必须是唯一的,在一个struts.xml文件中不能出现两个同名的包。extends属性继承一个默认的配置文件“struts-default”。
<action>标签中的name属性表示动作名,class表示动作类名。
<result>标签的name实际上就是execute方法返回的字符串,如果返回的是“success”,就跳转到welcome.jsp页面,如果是“error”,就跳转到hello.jsp页面。
MyEclipse 9.0还提供了.xml文件的可视化功能,由XML文件编辑工作区切换到左下角的【Flow】选项页(如图5.4),就可以看到这个struts.xml文件的可视化流程图
创建welcome.jsp
复制代码
1
2
3
4
5
6
7
8
9
10
11<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s" %> <html> <head> <title>struts 2应用</title> </head> <body> hello <s:property value="#request.name"/>! </body> </html>
部署和运行
最后
以上就是如意猎豹最近收集整理的关于struts的简单介绍的全部内容,更多相关struts内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复