一、Struts2是什么?
Struts2 是一个非常优秀的MVC框架,基于Model2 设计模型。
二、Struts2在开发中所处的位置?
三、Strust2 核心功能是什么?
(1)允许POJO(Plain Old Java Objects)(即JavaBean)对象 作为Action。
(2)Action的execute 方法不再与Servlet API耦合,更易测试。
(3)支持更多视图技术(JSP、FreeMarker、Velocity)。
(4)基于Spring AOP思想的拦截器机制,更易扩展。
(5)更强大、更易用输入校验功能。
四、开发人员用Struts2干什么?
1. 控制器:核心组件(框架提供的)
Servlet VS Filter
Servlet:
(1) 每一个Servlet内存中唯一。
(2) Load-on-startup就可以使Servlet在启动时就完成初始化。
(3) 用户的每次请求都会经过void service(request, response)
(4) 配置映射为/*时,所有的请求都会经过该Servlet。
Filter:
(1) 每一个Filter内存中唯一。
(2) 启动时就完成初始化。
(3) 用户的每次请求都会经过void doFilter(request , response)
(4) 配置映射为/*时,所有的请求都会经过该过滤器。
不同:过滤器要比Servlet强大,因为过滤器有权利决定是否放行。
总结:Servlet能做的,Filter都能做。
2. 配置文件:指挥控制器运作。(工作中经常写)
3. 动作类:处理逻辑。(工作中经常写)
4. JSP:展现结果。(工作中经常写)
五、Struts2的第一个简单的案例:
1。导包(13个),开发时可以使用Struts2包中的app/struts2-blank.war 携带jar包:
2。Struts2工程中各文件目录结构:
3。在web.xml文件中配置核心过滤器(控制器),即web.xml文件中的内容为:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<display-name></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.jsp</welcome-file>
</welcome-file-list>
</web-app>
4。建立struts.xml的配置文件,放在应用的类路径的顶端(即开发中放在src目录中),即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>
<!-- 开启开发模式 -->
<constant name="struts.devMode" value="true" />
<package name="p1" extends="struts-default">
<!-- 配置一个动作 -->
<action name="sayHello" class="com.itheima.action.HelloAction" method="sayHello">
<!-- 配置结果视图 -->
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
5.写动作类,HelloAction.java文件:
package com.itheima.action;
public class HelloAction {
public String sayHello(){
System.out.println("HelloAction的sayHello方法执行了");
return "success";
}
}
6。索引视图,index.jsp 文件:
<%@ page language="java" import="java.util.*" 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 href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="${pageContext.request.contextPath}/sayHello.action">第一个Struts2案例</a>
</body>
</html>
7。结果视图,success.jsp 文件:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 href="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
oyeah!!
</body>
</html>
8。测试:
运行:http://localhost:8080/Struts2_HelloWorld/index.jsp
结果:
点击超链接:浏览器中输出“oyeah!!”
控制台输出:HelloAction的sayHello方法执行了