Struts2框架簡介和示例


struts2框架

Struts2是java web的框架,在Java Web開發中,表示層框架,其核心是通過擴展Servlet來幫助處理http請求。

Struct2的基本流程

Struct2的框架由3個部分組成:核心控制器FilterDispatcher、業務控制器、和用戶實現的業務邏輯組件,其基本流程為:FilterDispatcher->Action->業務邏輯組件。核心控制器負責攔截所有的用戶請求,當請求是*.action結尾會被轉入Struts2框架處理,Struts2再決定調用哪個業務邏輯組件。業務控制器就是實現Action類的實例,Action類通常包含一個execute方法(也可在配置文件中指定方法執行),該方法返回一個邏輯視圖名的字符串。

Action

Struts2使用Action類封裝HTTP請求參數,因此Action類里包含與請求參數對應的屬性,並提供getter和setter方法。Action類通常包含一個execute方法,該方法返回一個為邏輯視圖名的字符串(error、none、input、login、success),Action類也可以在Struts2.xml的action元素中配置method屬性來指定方法調用。

Java Web開發環境

1、安裝jdk
2、安裝tomcat
3、配置環境

請戳:Java Web環境配置

Web工程

新建一個web工程,工程名為WebappTest

導入struts2的核心jar包

編寫Action類和配置文件

編寫action類
package com.example.action;
import java.io.Serializable;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport implements Serializable{
	
	/*
	*封裝用戶請求參數
	*提供getter和setter方法
	*/
	private String message;
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	//編寫struts2.xml指定的方法,返回邏輯視圖名稱
	public String sayHello(){
		message = "Hello Struts2";
		return SUCCESS;
	}
}

編寫action處理后跳轉的jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        ${message}
    </body>
</html>
配置action

Struts2使用package來管理Action和攔截器,每個包就是多個Action、多個攔截器及其引用的集合,定義pacckage元素是指定如下幾個屬性:

  • name:名字,可用於其他包的引用
  • extends:繼承至其他包
  • namespace:定義命名空間

使用result來配置結果,Struts2支持JSP、Velocity、FreeMaker多種視圖技術,這里的結果使用JSP視圖。

編寫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="song" namespace="/test" extends="struts-default">
			    <!--指定Action要調用的方法為sayHello-->
				<action name="hellostruts2" class="com.example.action.HelloAction" method="sayHello">
				    <!--為success的邏輯視圖配置Result-->
					<result name="success">/success.jsp</result>
				</action>
			</package>
		</struts>
	
在web.xml配置文件下添加配置項
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	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_3_0.xsd">
  <display-name></display-name>
  <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>

執行

開啟tomcat服務器,在瀏覽器中輸入

http://localhost:8080/WebappTest/test/hellostruts2

跳轉到jsp界面


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM