标签:config 容器 doctype 常量 copy package success templates 重定向
Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。
Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。其全新的Struts 2的体系结构与Struts 1的体系结构差别巨大。
Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开,所以Struts 2可以理解为WebWork的更新产品。
虽然从Struts 1到Struts 2有着太大的变化,但是相对于WebWork,Struts 2的变化很小。
体系结构
Struts 2资源包的目录结构
apps:官方提供的Struts 2应用实例
docs:官方提供的Struts 2文档
lib:Struts 2 发行包及其依赖包
src:Struts 2对应的源代码
Action接口中常量字符串的逻辑含义
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.struts.xwork</groupId>
<artifactId>xwork-core</artifactId>
<version>2.3.4.1</version>
</dependency>
配置Struts2
在web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
version="3.0">
<display-name>struts2_helloworld</display-name>
<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>
创建控制类HelloWorld
import com.opensymphony.xwork2.Action;
public class HelloWorld implements Action {
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
创建index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
User:
Date: 2018/11/15
Time: 9:26
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>====</h1>
</body>
</html>
配置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:包,用于对Action进行封装 name:包名,根元素下可以有多个包,彼此不能重名 extends:继承,用于指定继承的包,相当于将继承包下的配置信息复制到当前包 namespace:命名空间,用于规定Action的访问路径,必须“/”开头 -->
<package name="default" namespace="/" extends="struts-default">
<!--action:业务控制器,用于注册业务控制器组件 name:action名称,用于规定Action的访问路径 class:业务控制器组件,用于指定业务控制器对应的类 method:方法,用于指定访问当前action时要调用的方法 *请求URL:http://ip:port/projectName/namespace/ActionName.action -->
<action name="hello" class="action.HelloWorld ">
<!--
result:输出组件,用于转发、重定向、直接输出 name:名称,一个action下可以有多个result,彼此不能重名 默认值转发,元素内设置转发的页面 -->
<result name="success">/hh.jsp</result>
</action>
</package>
</struts>
效果图

实现通配符
public class PatternAction extends ActionSupport{
public String list(){
((Map<String,Object>)ActionContext.getContext().get("request")).put("msg","Result类型");
return "list";
}
public String add(){
return SUCCESS;
}
}
list.jsp
<html> <head> <title>Title</title> </head> <body> list </body> <html>
效果
标签:config 容器 doctype 常量 copy package success templates 重定向
原文地址:https://www.cnblogs.com/s122/p/9974716.html