? 什麼是Struts框架
–從不同的角度看待Struts框架
–Struts框架的優點
? 下載安裝Struts框架
–下載配置Struts框架
–測試Struts框架
–安裝Struts應用程序
–訪問Struts文檔
? Struts 中常用組件
–ActionServlet
–Action
–ActionForm
–ActionMapping
–ActionForward
? 實例
–以登錄爲例,比較servlet+jsp和Struts的區別和聯繫
–從不同的角度看待Struts框架
–Struts框架的優點
? 下載安裝Struts框架
–下載配置Struts框架
–測試Struts框架
–安裝Struts應用程序
–訪問Struts文檔
? Struts 中常用組件
–ActionServlet
–Action
–ActionForm
–ActionMapping
–ActionForward
? 實例
–以登錄爲例,比較servlet+jsp和Struts的區別和聯繫
-----------------------------START-----------------------------------
什麼是Struts框架(從不同的角度看待Struts框架)
? 一個MVC設計模式框架?
–Struts爲使用Servlet和JSP來開發Web應用程序提供了一個統一的框架
? 一個工具集合?
–Struts 提供了一系列工具集合,來處理Web開發中的常見任務
? 一個自定義標籤工具集合?
–Struts提供了一系列標籤,包括html、表單、bean、條件判斷等
?仁者見仁,智者見智
–但是Struts框架,最核心的稱呼還應該是MVC框架
–Struts爲使用Servlet和JSP來開發Web應用程序提供了一個統一的框架
? 一個工具集合?
–Struts 提供了一系列工具集合,來處理Web開發中的常見任務
? 一個自定義標籤工具集合?
–Struts提供了一系列標籤,包括html、表單、bean、條件判斷等
?仁者見仁,智者見智
–但是Struts框架,最核心的稱呼還應該是MVC框架
--------------------------NEXT----------------------------------------
Struts 框架的優點
? 基於配置文件的鬆耦合
–在Struts框架中的通常類都被配置在一個配置文件當中(如:Action、Formbean、ActionForward等),和寫在Java程序裏的硬代碼相比,耦合性降低了很多,在Servlet當中一般頁面的跳轉被寫在Java程序當中,例如:request.getRequestDispatcher(“somepage”).forward(request,resp
onse);
? Formbean的強大功能
–在傳統的Servlet+JSP當中,驗證信息的重新顯示必須有程序員來設置, 但是在Struts當中Formbean解決了這個問題。
? 集中的驗證
–Struts提供的標籤
? 基本html標籤
? form表單標籤
? bean標籤
? 邏輯標籤
? Struts的其他優點
–對國際化的支持、聲明式異常處理等。
–在Struts框架中的通常類都被配置在一個配置文件當中(如:Action、Formbean、ActionForward等),和寫在Java程序裏的硬代碼相比,耦合性降低了很多,在Servlet當中一般頁面的跳轉被寫在Java程序當中,例如:request.getRequestDispatcher(“somepage”).forward(request,resp
onse);
? Formbean的強大功能
–在傳統的Servlet+JSP當中,驗證信息的重新顯示必須有程序員來設置, 但是在Struts當中Formbean解決了這個問題。
? 集中的驗證
–Struts提供的標籤
? 基本html標籤
? form表單標籤
? bean標籤
? 邏輯標籤
? Struts的其他優點
–對國際化的支持、聲明式異常處理等。
----------------------------NEXT-----------------------------------
下載配置Struts框架
? Struts 框架的下載站點
– http://jakarta.apache.org/site/binindex.cgi
– http://jakarta.apache.org/struts
? 解壓下載包,瞭解Struts的目錄結構
–Lib:jar文件
–Webapps:web應用程序
? Struts 框架的下載站點
– http://jakarta.apache.org/site/binindex.cgi
– http://jakarta.apache.org/struts
? 解壓下載包,瞭解Struts的目錄結構
–Lib:jar文件
–Webapps:web應用程序
安裝Struts應用程序
? 安裝struts-blank.war
–將該壓縮包拷貝到tomcat的webapps目錄下,重新啓動tomcat,訪問該工程:
http://localhost:8080/struts-blanck
? 安裝struts-blank.war
–將該壓縮包拷貝到tomcat的webapps目錄下,重新啓動tomcat,訪問該工程:
http://localhost:8080/struts-blanck
? 安裝struts-documentation.war
–將該壓縮包拷貝到tomcat的webapps目錄下,重新啓動tomcat,訪問該工程:
http://localhost:8080/struts-documentation
------------------------NEXT--------------------------------
Struts 中的組件介紹
? ActionServlet
–Struts中的大控制器
–Struts框架的入口
–是對Servlet的封裝,被配置在web.xml配置文件當中
? Action
–小控制器,處理具體的業務邏輯用例
? ActionForm
–和表單對應的一個特殊的JavaBean,是一個“郵遞員”在控制器和頁面之間傳遞數據,也提供了一個集中的驗證方法
? ActionMapping
–從Struts配置文件中讀取配置信息
? ActionForward
–頁面之間的跳轉
–Struts中的大控制器
–Struts框架的入口
–是對Servlet的封裝,被配置在web.xml配置文件當中
? Action
–小控制器,處理具體的業務邏輯用例
? ActionForm
–和表單對應的一個特殊的JavaBean,是一個“郵遞員”在控制器和頁面之間傳遞數據,也提供了一個集中的驗證方法
? ActionMapping
–從Struts配置文件中讀取配置信息
? ActionForward
–頁面之間的跳轉
------------------------------NEXT--------------------------------------
Struts 實例(以用戶登錄爲例)
? Servlet+JSP 版本的登錄
–爲了比較servlet+jsp和Struts的區別與聯繫,我們首先看一下servlet+jsp如何創建用戶登錄
? Servlet+JSP 版本的登錄
–爲了比較servlet+jsp和Struts的區別與聯繫,我們首先看一下servlet+jsp如何創建用戶登錄
login.jsp
successfull.jsp
failure.jsp
LoginServlet.java
測試:
redking帳號登錄
登錄成功!
非redking帳號登錄
登錄失敗
-------------------------------------NEXT--------------------------------
下面通過數據庫查詢來驗證用戶登錄哈~
DB
UserDao.java
UserDaoImpl.java
LoginServlet.java
package com.redking.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.redking.dao.UserDao;
import com.redking.dao.impl.UserDaoImpl;
import com.redking.vo.User;
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
/*
if(username!=null&&username.equals("redking")){
request.setAttribute("username", username);
request.getRequestDispatcher("/pages/successfull.jsp").forward(request, response);
}else{
request.getRequestDispatcher("/pages/failure.jsp").forward(request, response);
}
*/
UserDao dao = new UserDaoImpl();
User u = dao.login(username, password);
HttpSession session = request.getSession();
if(u!=null){
session.setAttribute("user", u);
request.getRequestDispatcher("/pages/successfull.jsp").forward(request, response);
}else{
request.getRequestDispatcher("/pages/failure.jsp").forward(request, response);
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.redking.dao.UserDao;
import com.redking.dao.impl.UserDaoImpl;
import com.redking.vo.User;
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
/*
if(username!=null&&username.equals("redking")){
request.setAttribute("username", username);
request.getRequestDispatcher("/pages/successfull.jsp").forward(request, response);
}else{
request.getRequestDispatcher("/pages/failure.jsp").forward(request, response);
}
*/
UserDao dao = new UserDaoImpl();
User u = dao.login(username, password);
HttpSession session = request.getSession();
if(u!=null){
session.setAttribute("user", u);
request.getRequestDispatcher("/pages/successfull.jsp").forward(request, response);
}else{
request.getRequestDispatcher("/pages/failure.jsp").forward(request, response);
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
UserDaoImplTest.java
ConnectionUtil.java
SQLConstants.java
package com.redking.util;
public interface SQLConstants {
public static final String USER_LOGIN_SQL = " select id,username,password from UserTbl " + " where username= ? and password = ? ";
}
public interface SQLConstants {
public static final String USER_LOGIN_SQL = " select id,username,password from UserTbl " + " where username= ? and password = ? ";
}
User.java
DBConfig.properties
login.jsp
successfull.jsp
failure.jsp
測試:
登錄成功
測試數據庫中沒有的用戶登錄試試
測試結果
現在我們將51cto帳號加入DB再測試
51cto帳戶重新登錄
測試結果
--------------------------------NEXT-------------------------------------
? 使用Struts來開發用戶登錄
–將Struts添加到上述存在的工程,看一下Struts是怎麼樣運行的。
? 使用Struts來開發用戶登錄
–將Struts添加到上述存在的工程,看一下Struts是怎麼樣運行的。
加載Struts框架所需JAR包,加載到工程的LIB目錄中
導入ActionServlet配置文件
鬆耦合配置文件Struts-config.xml
生成最基本的配置模板
LoginAction.java
login.jsp
web.xml
測試:
登錄成功
再次測試錯誤密碼
登錄失敗
---------------------------------END-----------------------------------