国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Struts2入門案例

2019-11-08 02:05:23
字體:
來源:轉載
供稿:網友

一、Struts2是什么? Struts2是一個基于MVC設計模式的Web應用框架,相當于一個Servlet,Struts2作為一個Controller來建立模型與視圖的數據交互。Struts2以WebWork為核心,采用攔截器的機制來處理用戶請求。業務邏輯控制器和ServletAPI完全脫離。

二、Struts2的使用? 以struts-2.3.14為例: 1、下載Struts2的jar包。新建一個web項目并將需要的jar包導入。不同版本要的包不同,不知道導哪些包的時候,一個保險方法是將struts/apps文件夾下的struts2-blank.war包打開。將里面的WEB-INF/lib下的包導入自己的項目。

2、Struts2配置,配置web.xml文件。

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Struts2Demo</display-name> <!-- Struts2的過濾器,請求都被映射到Struts2上 --> <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> </web-app>

3、配置strute.xml文件,配置Action。(可以放在src下,也可以放在WEB-INF/classes下面)

<?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> <!-- 配置文件中,包名不重復即可。自己創建的包最好都繼承struts-default包,該包提供了絕大多數的功能--> <package name="struts2" extends="struts-default"> <!-- action元素與表單對應 --> <action name="login" class="fang.LoginAction"> <result name="success">/welcome.jsp</result><!-- result為action處理結果 --> <result name="error">/error.jsp</result> </action> </package> </struts>

4、寫一個對應的Action,繼承自ActionSupport。

import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private static final long serialVersionUID = 1L; private String name; private String passwd; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } @Override public String execute() { if ("admin".equals(name) && "admin".equals(passwd)) { return SUCCESS; } else return ERROR; } }

5、對應的JSP文件 (1)login.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title><s:text name="登陸頁面"/></title> </head> <body> <s:form action="login"><!--action的值對應于struts.xml文件中的<action ...> 中的name屬性--> <s:label value="請登錄"/><!-- 文字標簽--> <s:textfield name="name" key="用戶名"/><!--文本框--> <s:textfield name="passwd" key="密碼"/> <s:submit value="登錄"/> </s:form> </body> </html>

(2)welcom.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title><s:text name="歡迎頁面"/> </title> </head> <body> 歡迎您,<s:property value="name"/><!--顯示Action中的account屬性--> </body> </html>

(3)error.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title><s:text name="登錄失敗頁面"/> </title> </head> <body> <s:text name="登錄失敗。。。"/> </body> </html>

執行:http://localhost:8080/Struts2Demo/login.jsp

三、幾點說明 1、Struts2的工作流程: Struts2拋棄了request、response等Servle API。在調用Action的執行方法(如:execute)前,struts2 的會從request中獲取參數,并通過setter方法設置到Action屬性。

訪問到login.jsp后,找到表單要提交到的action:;Struts2截獲所有的請求; 通過action的值,在struts.xml配置文件中找到對應的Action。生成一個Action實例,并并將提交的數據設置到該實例中;調用execute()主方法; 根據執行完對應的Action后返回結果,struts.xml配置文件中/welcome.jsp /error.jsp 展現對應的jsp頁面。

2、Struts2中每個Action有多個實例(Struts1中所有的Action只有一個實例。)每處理一次請求生成一個實例,處理完即銷毀。因此Struts2是線程安全的。

3:Action: (1)自定義Action一般直接繼承自ActionSupport類,定義變量,重寫execute()方法。變量的值被Struts2通過setter方法自動復制,execute直接使用即可。 execute的執行結果返回值配置在中。 (2)事實上任何的POJO都可以作為Action,只要該Action有public String execute()方法。 這樣的好處是不與Struts2發生耦合,代碼不依賴Struts2類庫。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 敖汉旗| 仙游县| 辽阳市| 南江县| 湟源县| 焉耆| 绥棱县| 高尔夫| 双桥区| 琼中| 南汇区| 原阳县| 灌阳县| 绥阳县| 宜春市| 西昌市| 庆元县| 大余县| 高尔夫| 青岛市| 昌江| 曲麻莱县| 那坡县| 页游| 石狮市| 紫金县| 奇台县| 阿尔山市| 遂宁市| 银川市| 崇仁县| 漠河县| 堆龙德庆县| 米脂县| 运城市| 天等县| 綦江县| 思南县| 锦屏县| 思南县| 贺州市|