Struts 2 簡介 Struts2的基本流程
轉自:https://blog.csdn.net/zjtimef/article/details/12027767
Struts2的基本流程如下:
① Web瀏覽器 發送一個請求(HttpServletRequest)。
② 過濾器Dispatcher查找請求,確定適當的Action。
③ 攔截器自動對請求應用通用功能,如驗證和文件上傳等操作。
④ Action的execute方法通常用來存儲和(或)重新獲得信息(通過數據庫)。
⑤ 結果被返回到瀏覽器,可能是HTML、圖片、PDF或其他。
下麵以一個簡單的實例來說說struts2。
首先將strut2所需的jar包導入

接著修改web.xml文件,Struts1的入口點是一個Servlet,而Struts2的入口點是一個過濾器(Filter)。因此,Struts2要按過濾器的方式配置
[html]
view plaincopyprint?
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="https://java.sun.com/xml/ns/javaee"
- xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="https://java.sun.com/xml/ns/javaee
- https://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
[html]
view plaincopyprint?
- <%@ 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類繼承
[java]
view plaincopyprint?
- package 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類
[html]
view plaincopyprint?
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "https://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
[html]
view plaincopyprint?
- <%@ 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>
部署和運行

最後更新:2017-04-03 15:21:56