Web框架——Struts2

前言

Struts作爲MVC 2的Web框架,自推出以來不斷受到開發者的追捧,得到用廣泛的應用。
作爲最成功的Web框架,Struts自然擁有衆多的優點:
MVC 2模型的使用
功能齊全的標誌庫(Tag Library)
開放源代碼
但是,所謂“金無赤金,人無完人”,Struts自身也有不少的缺點:
需要編寫的代碼過多,容易引起“類爆炸”
單元測試困難
這些缺點隨着Web的發展越來越明顯。這就促生了Struts 2.0







環境搭建

前提

  • Java相關環境
  • maven插件已安裝

建立web項目

1、導入Struts2依賴

	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>${struts2.version}</version>
		</dependency>
	</dependencies>

2、配置web.xml

<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"
	id="WebApp_ID" version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  <!--struts2中央控制器  -->
  <filter>
  	<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>StrutsPrepareAndExecuteFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

3、配置struts.xml
struts.xml(核心配置文件):引入其他xml配置
struts.properties(全局屬性文件)
struts.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<include file="struts-default.xml"/>
	<include file="base.xml"></include>
    <include file="sys.xml"></include>
</struts>
	

base.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<!-- 屬性文件 以鍵和值的方式存在配置到xml裏面去    字符編碼  dev(開發)模式  改了配置文件會自動加載-->
	<constant name="struts.i18n.encoding" value="UTF-8" />
	<constant name="struts.devMode" value="true" />
	<constant name="struts.configuration.xml.reload" value="true" />
	<package name="base"  extends="struts-default" abstract="true">
		<global-allowed-methods>regex:.*</global-allowed-methods>
	</package>
</struts>

sys.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="sys" namespace="/sys" extends="base" >
	<action name="bookAction_*" class="com.zking.action.BookAction" method="{1}">
	<result name="list">/list.jsp</result>
	</action>
	</package>
</struts>

4.建立BaseAction

package com.zking.action;

import java.util.List;

import com.opensymphony.xwork2.ModelDriven;
import com.zking.base.BaseAction;
import com.zking.dao.BookDao;
import com.zking.entity.Book;
import com.zking.util.JsonData;
import com.zking.util.PageBean;

public class BookAction extends BaseAction implements ModelDriven<Book> {
	private BookDao bd=new BookDao();
	private Book b=new Book();
	@Override
	public Book getModel() {
		return b;
	}
	
	public String add() {
		JsonData jsonData=new JsonData();
		bd.addBook(b);
		jsonData.setCode(0);
		jsonData.setMessage("書本新增成功");
		this.toJson(jsonData);
		return null;
	}
	
	public String list() {
		PageBean pageBean=new PageBean();
		pageBean.setRequest(request);
		request.setAttribute("pageBean", pageBean);
		
		List<Book> listBook = bd.listBook(b, pageBean);
		request.setAttribute("listBook", listBook);
		System.out.println(listBook);
		return "list";
	}
}

5.在webapp下建立jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="z" uri="/zking"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>書籍查詢</h1>
	<table border="1" width="100%">
		<tr>
			<td>ID</td>
		    <td>書名</td>
		    <td>封面</td>
		    <td>價格</td>
		    <td>操作</td>
		</tr>
		<c:forEach var="v" items="${listBook }">
		<tr>
			<td>${v.bookId }</td>
		    <td>${v.bookName }</td>
		    <td>
		    <c:if test="${v.bookImage !=0 }">
		    <a href="<%=request.getContextPath()%>/sys/fileAction_saveAs.action?fileId=${v.bookImage }"><img style="width:80px;height:80px;" src="<%=request.getContextPath()%>/sys/fileAction_open.action?fileId=${v.bookImage }"></a>
		    </c:if>
		    </td>
		    <td>${v.bookPrice }</td>
		    <td><a href="<%=request.getContextPath()%>/bindBookImage.jsp?bookId=${v.bookId }">上傳封面</a>
		    <c:if test="${v.bookImage !=0 }">
		    <a href="<%=request.getContextPath()%>/sys/fileAction_saveAs.action?fileId=${v.bookImage }">另存爲</a>
		    </c:if>
		    </td>
		</tr>
		</c:forEach>
	</table>
	<z:page pageBean="${pageBean }"/>
</body>
</html>

好啦,初始學習就到這裏啦,可以直接複製代碼測試玩一下
個人認爲struts2需要掌握的重點包含:

  • XML是什麼含義
  • struts2的常用標誌
  • 關於struts2的action理念
  • struts2攔截器的工作原理
  • 在struts2中實現IOC、文件上傳、CRUD
  • struts2中的OGNL表達式
  • struts2和ajax如何交互
    通過學習struts2你要明白的幾個概念:
    反射的原理、MVC架構、xml建模

其他知識點

OGNL

1 OGNL的全稱是Object Graph Navigation Language(對象圖導航語言),它是一種強大的表達式語言
2 OgnlContext(ongl上下文)其實就是Map<String,Object>
OgnlContext=根對象(1)+非根對象(N)
非根對象要通過"#key"訪問,根對象可以省略"#key"
注意:context:英文原意上下文,容器
3 把根對象和非根對象說出來
附:struts2中ODNL結構圖
在這裏插入圖片描述






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章