Struts框架搭建步骤
1.Maven 专家 内行
Apache下一个子项目,项目管理工具,构件,依赖管理
POM.xml:Project Object Model 项目对象模型
配置:jar依赖
2.下载Maven,配置本地仓库,配置中心仓库为阿里服务器。
$Maven_home/conf/settings.xml
配置本地仓库:
<localRepository>F:\maven\repo</localRepository>
更换为阿里的服务器镜像:
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
3.在eclipse中配置Maven插件。指向上一步我们自己的Maven和仓库。
eclipse–windows–Preferences–搜Maven–User settings
4.创建maven的web应用项目。
new project–maven project -使用maven模型(Ineteral–过滤web–maven–archetype–webapp)
groupid–一般填入包名—组ID
artifactId----项目名 — 坐标ID
注意:默认jsp报错,没有servlet的jar.
解决方法:修改pom.xml,添加依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
5.配合和搭建jetty服务器,轻量级的web容器。运行我们的web项目。
注意:这一步可以使用Tomcat替代。
在pom.xml添加jetty插件
在build节点下添加:
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.7.v20170914</version>
<configuration>
<webApp>
<contextPath>/${build.finalName}</contextPath>
</webApp>
<stopKey>CTRL+C</stopKey>
<stopPort>8999</stopPort>
<scanIntervalSeconds>10</scanIntervalSeconds>
<scanTargets>
<scanTarget>src/main/webapp/WEB-INF/web.xml</scanTarget>
</scanTargets>
</configuration>
</plugin>
</plugins>
6.发布和运行jetty
选中项目或pom.xml右键-run as-maven build – 在Goals:输入:jetty:run–运行即可
打开浏览器,输入url测试。
**********************开始搭建struts项目
7.先在pom.xml中添加struts2的依赖
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
</dependency>
8.添加log4j(不是必须)
a.在src/main/resources下新建文件log4j2.xml。
b.按官网配置
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="com.opensymphony.xwork2" level="debug"/>
<Logger name="org.apache.struts2" level="debug"/>
<Root level="warn">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
b.添加log4j依赖在pom.xml中。
9.最关键的步骤,配置struts的核心控制器Filter.
在web.xml中,配置过滤器
<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>
10.新建strut.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.devMode" value="true" />
<package name="basicstruts2" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
</package>
</struts>
使用struts案例***
1.新建add.jsp作为视图层
2.新建action作为控制层
a.注意:maven中的源代码位置在src/main/java下面,所以要先新建目录
(约定大于配置)
b.新建类 AddAction
c.声明方法execute(),接收参数的属性,并给定getter和setter方法
3.给action做url映射配置
a.增加及子节点
b.name的值对应页面请求的url,class对应action的类
<action name="add" class="com.wdzl.action.AddAction" >
<!--跳转的结果视图 success逻辑名对应了index.jsp-->
<result name="success">/index.jsp</result>
<result name="error">/error.jsp</result>
</action>
4.运行测试
1.Action的实现方式:
a.普通类
b.实现Action接口
c.继承ActionSupport 推荐
- 封装实体类
- 1.实现ModelDriven接口
- a.action实现接口ModelDriven并指定类型泛型
- b.声明属性为实体类并实例化赋值
- c.重写实现getModel的方法,返回实体属性。
- 2.getter和setter方法,注意页面表单元素命名
3.操作隐式对象
1.实现接口方式RequestAware,SessionAware
2.上下文方式