Java Web:STRUTS2基本配置,筆記,例子,講義,視頻,OGNL,pojo類【詩書畫唱】
本期看點:視頻和視頻筆記、?講義、例子、Struts2框架概述PPT、OGNL、pojo類
友情提示:通過訪問站點http://struts.apache.org/download.cgi#struts2320,可下載最新版本的Struts2框架的核心包。
視頻和視頻筆記START




視頻和視頻筆記END
講義START

一、JAVA框架介紹
二、STRUTS2框架
JAVA:分為三大部分
JAVASE:JAVA基礎(chǔ),基本語法和OOP
JAVAEE:企業(yè)級開發(fā),十三種技術(shù)規(guī)范,很多的JAVA框架
JAVAME:移動端開發(fā),anroid開發(fā)需要java基礎(chǔ)
JAVA框架中的三種典型框架:自學(xué)和舉一反三的能力
struts1,struts2框架,springmvc框架:都是同一類的框架
springcore框架
hibernate框架,mybatis框架:都是同一類框架
框架都是項目開發(fā)的工具,框架沒有那么難學(xué)
struts2框架:項目中的servlet會升級,看不到request和response了,getParameter消失了
springcore框架:理解框架和框架之間的粘合劑。
mybatis框架:數(shù)據(jù)庫框架,這個框架可以讓我們不再編寫jdbc代碼,dao類的代碼全部消失了。
我們可以從以上的三種框架中各自選出一種框架,使用到我們的項目中。那么這個項目就是一個組合框架的項目。
微服務(wù):springcloud
S4:js前端技能
java后臺技能,背景調(diào)查,人工智能,大數(shù)據(jù)
Struts2框架中的所有的servlet會被升級為action(普通的java類)
在項目中使用Struts2框架的步驟:
1、導(dǎo)入struts2的框架包
2、打開web.xml文件,配置struts2框架的核心過濾器
3、將struts2框架的核心配置文件拷貝到src目錄下
創(chuàng)建一個action,在后臺能夠打印出hello world,跳轉(zhuǎn)到歡迎頁面index.jsp
1、創(chuàng)建一個普通的java類,創(chuàng)建一個名為execute,返回值為String的無參的方法
2、在struts.xml文件中進行配置
3、啟動服務(wù)器
創(chuàng)建一個action,在后臺打印1+2等于多少,然后跳轉(zhuǎn)到show.jsp頁面
講義END
例子START
在項目中使用Struts2框架的步驟:




可以“點”出(利用其提示)其內(nèi)容。


訪問action的方法:


跳轉(zhuǎn)界面的方法START



跳轉(zhuǎn)界面的方法END


package com.jy.action;
public class AddAction {
//http://localhost:8888/J190802/pub/addAc.action
? ? public String execute(){
? ? System.out.println(1 + 2);
? ? return "sh";
? ? }
}


package com.jy.action;
//action就是一個普通的java類
public class HelloAction {
//方法簽名不能變
//返回值必須是String
//方法名必須是exectue
//沒有參數(shù)
//http://localhost:8888/J190802/pub/hlAc.action
? ? public String execute(){
? ? System.out.println("Hello world");? ?
? ? return "wel";
? ? }
}


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
? ? <package name="my" namespace="/pub" extends="struts-default">
? ? ? ? <action name="hlAc" class="com.jy.action.HelloAction">
? ? ? ? ? ? <result name="wel">/index.jsp</result>
? ? ? ? </action>
? ? ? ? <action name="addAc" class="com.jy.action.AddAction">
? ? ? ? ? ? <result name="sh">/show.jsp</result>
? ? ? ? </action>
? ? </package>? ? ? ?
</struts>


<?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" id="WebApp_ID" version="3.0">
? <display-name>J190802</display-name>
? <!-- 將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>
? <welcome-file-list>
? ? <welcome-file>index.html</welcome-file>
? ? <welcome-file>index.htm</welcome-file>
? ? <welcome-file>index.jsp</welcome-file>
? ? <welcome-file>default.html</welcome-file>
? ? <welcome-file>default.htm</welcome-file>
? ? <welcome-file>default.jsp</welcome-file>
? </welcome-file-list>
</web-app>


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? </head>
? ? <body>
? ? ? ? <h1>歡迎使用Struts2框架</h1>
? ? </body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? </head>
? ? <body>
? ? ? ? <h1>哇,好聰明</h1>
? ? </body>
</html>
例子END
Struts2框架概述PPT? START





dispatcher
調(diào)度員常用釋義英 [d??sp?t??(r)]美 [d?s?p?t??]
n.
調(diào)度員;[計] 調(diào)度程序;[計] 分配器

Struts2的action可以直接是一個pojo類

支持OGNL表達



描述:HttpServletRequest請求到達后首先經(jīng)過ActionMapper,然后經(jīng)過FilterDispatcher,如果還有其他的Filter,一次經(jīng)過,最后到達一個叫ActionProxy的地方,這個時候通過ConfigurationManager讀取配置文件struts.xml,然后到達ActionInvocation,開始經(jīng)過一個個的Interceptor,最后達到Action進行處理后返回結(jié)果。


通過訪問站點http://struts.apache.org/download.cgi#struts2320可下載最新版本的Struts2框架的核心包。







類似于servlet,訪問struts的action路徑為:http://[服務(wù)器端ip地址]:[服務(wù)器配置文件中指定的端口號]/[工程名][struts配置文件中的namespace屬性]/[struts配置文件中需要訪問的action的name屬性].action。

Struts2框架概述PPT? END