目录
- 环境搭建
- 开发
- 核心文件配置
1.环境搭建
1.1 jar
1.2 web.xml
1.3 struts.xml
在搭建好Maven的环境配置之后用eclipse创建一个Maven项目
导入struts依赖jar包
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
配置pom.xml
-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.Lix</groupId>
<artifactId>Lix_struts</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Lix_Struts Maven Webapp</name>
<url>http://maven.apache.org</url>
-<dependencies>
-<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
-<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
-<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
-<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
<!-- 5.3、jstl、standard -->
-<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
-<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- 5.4、tomcat-jsp-api -->
-<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>8.0.47</version>
</dependency>
</dependencies>
-<build>
<finalName>Lix_Struts</finalName>
-<plugins>
-<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
-<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置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>
<!-- struts框架自带的核心类的配置(struts-default.xml) -->
<include file="struts-default.xml"></include>
<!-- 配置struts全局设置 -->
<include file="struts-base.xml"></include>
<!-- 将系统开发的每一个模块分门别类 便于模块action的查找 -->
<include file="struts-sy.xml"></include>
</struts>
struts-sy.xml
<!--
相对mvc的差异性
package:用来将一类子控制器进行分类
http://localhost:8080/Lix_Struts/sy/user_add.action
中/sy对应的namespace="/sy"
extends:包的继承
*的含义:
*代表任意方法,只要前台浏览器匹配/user_*这一格式,那么user_add中,*代表了add
-->
<package name="sy" extends="base" namespace="/sy">
<action name="/user_*" class="com.Lix.one.web.UserAction" method="{1}">
<result name="success">/test.jsp</result>
</action>
</package>
</struts>
struts-base.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.i18n.encoding" value="UTF-8" />
<constant name="struts.devMode" value="true" /><!-- devMode开启动态方法 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- struts.configuration.xml.reload配置重载 -->
<constant name="struts.i18n.reload" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="base" extends="struts-default" abstract="true">
<global-allowed-methods>regex:.*</global-allowed-methods>
</package>
</struts>
2. 开发
2.1 Action
2.1.1 不需要指定父类(ActionSupport)
2.1.2 业务方法的定义
public String xxx();//execute
== 2.1.3 Action是多例模式(注:在spring中的配置中一定要注意)==
Action用来接收参数
2.2 参数赋值
2.2.1 Action中定义属性,并提供get/set方法
userName, getUserName/setUserName
2.2.2 ModelDriven
返回实体,不能为null,不需要提供get/set方法
2.2.3 ModelDriven返回实体和Action中属性重名,ModelDriven中优先级更高
注:ognl,ActionContext学完就知道了
2.3 与J2EE容器交互
2.3.1 非注入
2.3.1.2 耦合
ServletActionContext
2.3.1.2 解耦(建立使用解耦模式)
ActionContext
--src
--struts.xml(核心配置文件)
--struts.properties(全局属性文件)
3. 核心文件配置
3.1 include 包含文件
file
** 3.2 package 包**
name 包名
extends 继承
namespace 虚拟路径
abstract 通常用来被继承
** 3.3 action 子控制器**
name:helloAction,helloAction_*
class 全限定名
method:execute,{1}