目录

​一、新建基本环境(jar包与核心配置)​

​1.1新建web project,设置如下:​

​1.2拷贝jar包​

​1.3web.xml里加上核心过滤器的配置​

​二、编写前端请求链接与action类​

​2.1编写前端请求链接​

​2.2编写action类​

​2.3编写struts.xml配置文件 将请求与action具体方法对应匹配起来​

​三、测试运行​

​详细学习篇​

​struts.xml配置文件写法详述:​

​action类详述​

​1.默认方法:execute  ​

​2.action类最常用的继承写法​

​3.xml中action最常用的通配符配置方法​

​总结小模板★:​

​1.新建项目,导入jar包​

​2.添加核心配置​

​web.xml​

​3.编写action类​

​DemoAction.java​

​4.编写xml配置文件​

​struts.xml​

​5.编写前端测试链接​

​6.测试​


 

资料下载地址: ​​直接下载struts2_01.zip​​​       ​​网盘备份下载​

 

一、新建基本环境(jar包与核心配置)

1.1新建web project,设置如下:

ssh备考-04Struts2搭建Struts2框架_struts

1.2拷贝jar包

ssh备考-04Struts2搭建Struts2框架_struts_02

 

1.3web.xml里加上核心过滤器的配置

<!-- 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>

ssh备考-04Struts2搭建Struts2框架_xml_03

 

二、编写前端请求链接与action类

2.1编写前端请求链接

新建demo1.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>demo1</title>
</head>
<body>
<h3>struts2快速入门</h3>
<a href="${pageContext.request.contextPath }/hello.action">快速入门</a>
</body>
</html>

ssh备考-04Struts2搭建Struts2框架_xml_04

2.2编写action类

action类用来处理客户请求

package cn.ahpu.action;

/**
* struts2框架专门用来处理请求的action类
*/
public class HelloAction {

/**
* action类中具体处理某个请求的方法
* 方法必须满足3个条件: 公有public 无参 String类型返回
* 即是: public String xxx(){return xxString;}
*/
public String sayHello(){
System.out.println("hello struts2");
return "ok";//返回值控制:请求转发或者重定向的路径 return null表示不用跳转
}
}

ssh备考-04Struts2搭建Struts2框架_struts_05

2.3编写struts.xml配置文件 将请求与action具体方法对应匹配起来

注意:struts.xml文件位置必须在src更目录下

<?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>
<package name="hello" namespace="/" extends="struts-default">
<action name="hello" class="cn.ahpu.action.HelloAction" method="sayHello">
<!-- 重定向或请求转发路径都不包含:项目名 -->
<result name="ok">/demo1/suc.jsp</result>
</action>
</package>
</struts>

ssh备考-04Struts2搭建Struts2框架_struts_06

 

三、测试运行

访问​​http://localhost:8080/struts2/demo1/demo1.jsp​

控制台若打印出:hello struts2   则框架搭建成功

ssh备考-04Struts2搭建Struts2框架_struts_07

 

 

 

详细学习篇

struts.xml配置文件写法详述:

1. <package>标签,如果要配置<Action>的标签,那么必须要先配置<package>标签,代表的包的概念
* 包含的属性
* name -- 包的名称,要求是唯一的,管理action配置
* extends -- 继承,可以继承其他的包,只要继承了,那么该包就包含了其他包的功能,一般都是继承struts-default
* namespace -- 名称空间,一般与<action>标签中的name属性共同决定访问路径(通俗话:怎么来访问action),常见的配置如下
* namespace="/" -- 根名称空间 开发用这个就够了
* namespace="/aaa" -- 带有名称的名称空间
* abstract -- 抽象的。这个属性基本很少使用,值如果是true,那么编写的包是用来被继承的

2. <action>标签
* 代表配置action类,包含的属性
* name -- <package>标签的namespace属性一起来决定访问路径的
* class -- 配置Action类的全路径(默认值是ActionSupport类 但是肯定要写的,不能省略)
* method -- Action类中执行的方法,如果不指定,默认值是execute(默认执行方法名为execute的方法)

3. <result>标签
* action类中方法执行,返回的结果跳转的页面
* name -- 结果页面逻辑视图名称
* type -- 结果类型(默认值是转发,也可以设置其他的值)

<struts>
    <package name="名称唯一即可" namespace="/" extends="struts-default">
        <action name="访问名" class="对应action类的全路径" method="具体访问action类下哪个方法">
            <result name="返回值代号" type="转发还是重定向等">跳转路径</result>
        </action>
    </package>
</struts>

eg:上例子的配置
    <package name="hello" namespace="/" extends="struts-default">
        <action name="hello" class="cn.ahpu.action.HelloAction" method="sayHello">
            <result name="ok">/demo1/suc.jsp</result>
        </action>
    </package>

 表示一点击:超链接"项目名/hello.action"  就会执行cn.ahpu.action.HelloAction类的sayHello方法,然后跳转到"项目名/demo1/suc.jsp"
     / 来自namespace
     hello.action来自action标签的name值(.action可以省略,加上比较有框架的味道)
     执行哪个action类  class指定
     执行class类的哪个方法 method指定
     如何跳转:根据返回值与哪个result的name值相同匹配决定
     跳转方式:result的type属性值决定 默认转发
     跳转路径:result标签中间的值为路径

 

action类详述

1.默认方法:execute  

ssh备考-04Struts2搭建Struts2框架_xml_08

ssh备考-04Struts2搭建Struts2框架_xml_09

 

2.action类最常用的继承写法

Action类可以去继承ActionSupport类,开发中这种方式使用最多 以后都用这种方式有好多好用的方法以及常量

常量就5个,简单列举下(只是一种写返回值的规范):
    SUCCESS        -- 成功.
    INPUT            -- 用于数据表单校验.如果校验失败,跳转INPUT视图.
    LOGIN            -- 登录.
    ERROR            -- 错误.
    NONE            -- 页面不转向.

public class Demo2Action extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("我是继承了ActionSupport的action的默认方法execute");
return NONE;
}
}

 

3.xml中action最常用的通配符配置方法

<!-- 2.通配符的方式 
method="{数字} 数字表示action的name值中第几个*(从1开始数)
比如现在有个请求 /linkman_save 那么*会被解析为save {1}也就是save了 Action类里的save方法就被执行了(当然你要保证save方法存在)
-->
<action name="linkman_*" class="com.itheima.action2.LinkmanAction" method="{1}">
<result name="saveOK">/Demo1/success.jsp</result>
<result name="delOK">/Demo1/success.jsp</result>
</action>

 

总结小模板★:

1.新建项目,导入jar包

ssh备考-04Struts2搭建Struts2框架_struts_10

ssh备考-04Struts2搭建Struts2框架_struts_11

2.添加核心配置

web.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">

<!-- 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>

3.编写action类

DemoAction.java

package cn.ahpu.action;

import com.opensymphony.xwork2.ActionSupport;

public class DemoAction extends ActionSupport {

public String save(){
System.out.println("保存成功!");
return SUCCESS;
}

public String update(){
System.out.println("修改成功!");
return SUCCESS;
}

public String delete(){
System.out.println("删除成功!");
return SUCCESS;
}

}

 

4.编写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>
<package name="demo" namespace="/" extends="struts-default">
<action name="student_*" class="cn.ahpu.action.DemoAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>

 

5.编写前端测试链接

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>struts01demo</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/student_save.action">保存</a>
<a href="${pageContext.request.contextPath }/student_update.action">修改</a>
<a href="${pageContext.request.contextPath }/student_delete.action">删除</a>
</body>
</html>

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>struts01demo</title>
</head>
<body>
<h1>操作成功并成功跳转!</h1>
</body>
</html>

6.测试

完成后如下:

ssh备考-04Struts2搭建Struts2框架_xml_12

ssh备考-04Struts2搭建Struts2框架_struts_13