入门
下载地址:https://struts.apache.org/
解压后的目录
创建web工程,配置Tomcat,导入包
HelloAction
package com.company.hello;
public class HelloAction {
public String hello() {
System.out.println("hello world!");
return "success";
}
}
src下创建 struts.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="hello" namespace="/hello" extends="struts-default">
<action name="HelloAction" class="com.company.hello.HelloAction" method="hello">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置struts2核心过滤器-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
创建 hello.jsp
<%--
Created by IntelliJ IDEA.
User: mac
Date: 2019/3/9
Time: 5:27 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>hello world</h1>
</body>
</html>
浏览器输入 http://localhost:8080/hello/HelloAction
struts2.xml配置
<struts>
<!-- package:将Action配置封装.可以在Package中配置很多action.
name: 包名,标识作用,不能其他包名重复
namespace: 给action的访问路径中定义一个命名空间
extends: 继承一个 指定包
abstract: 包是否为抽象的; 标识性属性.标识该包不能独立运行.专门被继承
-->
<package name="hello" namespace="/hello" extends="struts-default" >
<!-- action:配置action类
name: 决定了Action访问资源名.
class: action的完整类名
method: 指定调用Action中的哪个方法来处理请求
-->
<action name="HelloAction" class="com.company.hello.HelloAction" method="hello">
<!-- result:结果配置
name: 标识结果处理的名称.与action方法的返回值对应.
type: 指定调用哪一个result类来处理结果,默认使用转发.
标签体:填写页面的相对路径
-->
<result name="success" type="dispatcher" >/hello.jsp</result>
</action>
</package>
</struts>
struts2常量配置
修改struts2常量配置
方式先后,也是配置文件的加载顺序,后者会覆盖先加载的
方式一:struts.xml
<struts> <constant name="struts.i18n.encoding" value="UTF-8"></constant> </struts>
方式二:strtus.properties
struts.i18n.encoding=UTF8
方式三:web.xml
<context-param> <param-name>struts.i18n.encoding</param-name> <param-value>UTF-8</param-value> </context-param>
常量配置
<struts>
<!-- i18n:国际化. 解决post提交乱码 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 指定反问action时的后缀名
http://localhost:8080/hello/HelloAction.do
-->
<constant name="struts.action.extension" value="do"></constant>
<!-- 指定struts2是否以开发模式运行
1.热加载主配置.(不需要重启即可生效)
2.提供更多错误信息输出,方便开发时的调试
(开发设置true,上架设置false)
-->
<constant name="struts.devMode" value="true"></constant>
<!-- 引入其他struts配置文件 -->
<include file="com/company/hello/struts.xml"></include>
</struts>
动态方法调用
方式一:
配置 struts.enable.DynamicMethodInvocation
<!-- 配置动态方法常量,默认是关闭的,需要开启 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
配置 global-allowed-methods
<global-allowed-methods>regex:.*</global-allowed-methods>
struts.xml 如下
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- 配置动态方法常量,默认是关闭的,需要开启 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="dynamic" namespace="/dynamic" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<!--或把方法用逗号分开-->
<!--<global-allowed-methods>add,delete</global-allowed-methods>-->
<action name="Demo" class="com.company.hello.Demo">
<result name="add">/add.jsp</result>
<result name="delete">/delete.jsp</result>
</action>
</package>
</struts>
Demo 类
package com.company.hello;
public class Demo {
public String add() {
System.out.println("add");
return "add";
}
public String delete() {
System.out.println("delete");
return "delete";
}
}
访问方式为 http://localhost:8080/dynamic/Demo!delete.do
访问方式为 http://localhost:8080/dynamic/Demo!add.do
方式二:通配符
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="dynamic" namespace="/dynamic" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="Demo_*" class="com.company.hello.Demo" method="{1}">
<result name="add">/add.jsp</result>
<result name="delete">/delete.jsp</result>
</action>
</package>
</struts>
访问方式为:
http://localhost:8080/dynamic/Demo_add.do
http://localhost:8080/dynamic/Demo_delete.do
默认配置
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" namespace="/default" extends="struts-default">
<!--如果找不到包下的action,会使用Demo作为默认action处理请求-->
<default-action-ref name="Demo"></default-action-ref>
<!-- method属性默认值 execute -->
<!-- class默认值 com.opensymphony.xwork2.ActionSupport -->
<action name="Demo" class="com.company.demo.Demo" method="add">
<!-- name默认属性 success -->
<!-- type默认值 dispatcher -->
<result name="add">/add.jsp</result>
</action>
</package>
</struts>
action类
方式1:不用继承任何父类,也不需要实现任何接口
package com.company.action;
public class Demo {
}
方式2:实现一个接口Action
package com.company.action;
import com.opensymphony.xwork2.Action;
public class Demo2 implements Action {
@Override
public String execute() throws Exception {
return null;
}
}
方式3:继承一个类 ActionSupport
package com.company.action;
import com.opensymphony.xwork2.ActionSupport;
public class Demo3 extends ActionSupport {
}