下载Struts的开发环境
官网:
https://struts.apache.org/download.cgi
解压下载下来的压缩包
apps:Struts2提供的应用,war文件:web项目打成war包。直接放入到tomcat可以运行。
docs :Struts2的开发文档和API
lib :Strtus2框架的开发的jar包
src :Struts2的源码
创建web项目,引入jar包
引入jar包--------------struts2-blank项目下找jar包(apps目录下)
创建jsp页面
编写Action类
HelloAction.java
/**
* Struts2的入门的Action类
* @author zhang
*
*/
public class HelloAction {
/**
* 提供一个方法:
* * 方法签名固定的
* 共有的返回值是String类型 方法名execute 在这个方法中不能传递参数
*/
public String execute() {
System.out.println("HelloAction...");
return "success";
}
}
对Action进行配置
在src下创建(提供)名称叫做struts.xml的配置文件
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>
<!-- Struts2为了管理Action的配置,通过包进行管理 -->
<!-- 配置Struts2的包============== -->
<package name="demo1" extends="struts-default" namespace="/">
<!-- 配置Action============= -->
<action name="hello" class="com.struts.demo1.HelloAction">
<!-- 配置页面的跳转========== -->
<result name="success">/demo1/success.jsp</result>
</action>
</package>
</struts>
配置核心过滤器
wen.xml
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts_day01</display-name>
<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>
<!-- 配置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>
</web-app>
编写success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>跳转成功页面!!!</h1>
</body>
</html>
Struts2的执行流程
当用户访问某一个Action的时候,先经过核心过滤器,在核心过滤器中执行一组拦截器(这组拦截器实现部分功能),执行目标Action,根据Action的返回值,进行页面跳转。
xml提示
和hibernate 类似