struts2框架搭建(一)
struts2是一个基于mvc的web应用框架。struts2本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器层(Controller)来建立模型与视图的数据交互。
struts2使用优势:自动封装参数、参数校验、结果的处理(转发|重定向)、国际化、显示等待页面、表单防止重复提交等。
struts2具有更加先进的架构以及思想。
struts2的历史
struts2与struts1的区别:struts1是基于servlet开发的,servlet本身就是线程不安全的,所以导致struts1存在很多问题。struts2是基于WebWork架构的完全是另一个架构了。
搭建struts2框架
https://mirrors.tuna.tsinghua.edu.cn/apache/struts/2.5.16/struts-2.5.16-min-lib.zip
1.新建项目 左侧选择java Enterprise 勾选Web Application中的Struts2 默认选择download 自己会下载相关jar包(最好自己下载,idea给下载的会少文件) ->finish。
2.command+; artifacts -> fix修复问题。
3.新版本没有web-inf文件夹 解决方法 进入project structure ->facets->点击小加号,添加web.xml 注意路径 项目名/web/WEB-INF/web.xml
4.在src中新建简单action类 HelloAction
package com.struts2.web;
public class HelloAction {
public String hello(){
System.out.println("hello david");
return "success";
}
}
5.在核心配置文件src/struts.xml中 配置hello方法
<?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>
<!--package用于封装多个action
name:起个名字,不能重复
namespace:定义命名空间 一级路径 /hello
extends:继承一个指定包
abstract:标示属性 是否是抽象类 给人看的专门给包继承的 不能独立运行
-->
<package name="hello" namespace="/hello" extends="struts-default">
<!--action配置action类
name:访问资源名 二级路径/hello/HelloAction
class:哪个action类的
method:action中的哪个方法
-->
<action name="HelloAction" class="com.struts2.web.HelloAction" method="hello">
<!--result结果配置
name:结果处理的名称,与action返回值对应
type: (默认使用转发)指定哪个result类来处理
标签体:页面的相对路径
-->
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
6.将struts2核心过滤器配置到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_3_1.xsd"
version="3.1"> <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> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> </web-app>
7.测试 启动项目
输入网址 http://localhost:9999/hello/HelloAction 见控制台输出 hello david
struts2访问流程
1.浏览器 访问http://localhost:9999/hello/HelloAction 地址
2.首先会访问web.xml中的过滤器
3.过滤器会去找 我们的核心配置文件struts.xml
4.首先拿到/hello去找每个package标签的namespace 看有没有/hello
5.如果找到了 就继续寻找package下的action 有没有name为HelloAction 的
6.找到action后 查看class属性 有完整包名 就可以通过反射 创建这个action对象 再去找后面的 method标签中的hello方法 并且调用该方法
7.调用方法后返回success,用返回值去找result标签 有没有success的结果 如果有 转发到/index.jsp页面
struts2架构
1.StrutsPrepareAndExecuteFilter 配置为/* 所有请求都要经过struts这个过滤器
2.过滤器会去找ActionMapper 处理请求信息 返回ActionMapping对象
3.获得ActionMapping对象后他回去找ActionProxy代理对象
4.ActionProxy去找ConfigurationManager配置文件管理器 去读取配置src/struts.xml
5.ActionProxy有了ActionMapping 又有了配置文件信息 交给了ActionInvocation 来处理后续问题
6.ActionInvocation处理interceptor集合 Action 后交给Result 去找jsp进行转发 回到浏览器 完成响应
struts2常量配置
默认常量配置存储在
struts-2.5.16/lib/struts2-core-2.5.16.jar!/org/apache/struts2/default.properties
修改默认常量配置
1.在src中新建struts.properties 把需要改的常量粘贴过来
2.(推荐)在struts.xml中 添加<constant name="" value="" /> 来添加需要配置的常量
3.在web.xml中 添加<context-param><param-name /><param-value /></context-param> 来添加需要配置的常量
常量配置有哪些
<?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> <!-- 指定Web应用的默认编码集,相当于request.setCharacterEncoding() 解决post乱码 -->
<constant name="struts.i18n.encoding" value="UTF-8" /> <!--设置访问路径 http://localhost:9999/hello/HelloAction.do-->
<constant name="struts.action.extension" value="do" /> <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" /> <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开-->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true" />
</struts>
动态方法调用
当一个userAction中有4 5 个方法时,如果静态配置,就需要写4 5 份配置节点。使用动态方法调用只配置一份即可。
方式1.在struts.xml中开启动态方法,然后action时 不设置method值,其中global-allowed-methods 是解决struts2.55以后版本的 方法授权问题。
<struts>
...
<!--开启动态方法 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <package name="User" namespace="/User" extends="struts-default">
<global-allowed-methods>Add,Delete,Update,Get</global-allowed-methods>
<action name="User" class="com.struts2.web.UserAction">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
然后编写UserAction类的方法
package com.struts2.web;
public class UserAction {
public String Add(){
return "success";
}
public String Get(){
return "success";
}
public String Update(){
return "success";
}
public String Delete(){
return "success";
}
}
测试:在浏览器中输入 http://localhost:9999/User/User!Delete 访问规则是/namespace/name!方法名
此方式不利于seo优化。
方式2.不需要配置动态方法常量,使用通配符*
<struts>
...
<package name="User" namespace="/User" extends="struts-default">
<global-allowed-methods>Add,Delete,Update,Get</global-allowed-methods>
<action name="User_*" class="com.struts2.web.UserAction" method="{1}">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
测试:在浏览器中输入 http://localhost:9999/User/User_Delete 访问规则就是/namespace/name_方法名
此方法常用!
引入多配置文件,include时文件都是相对于 classpath的 要写全路径
<include file="xml路径"></include>
默认值
<package name="hello" namespace="/hello" extends="struts-default">
<!--method:默认值execute
result的name:默认值success
result的type:默认值dispatcher
class:默认值com.opensymphony.xwork2.ActionSupport
-->
<action name="HelloAction" class="com.struts2.web.HelloAction" method="hello">
<result name="success">/index.jsp</result>
</action>
</package>
action类创建的方式
方式1.
//创建一个类,可以是POJO
//POJO:不用继承任何父类,也不需要实现任何借口
//使struts2框架的代码侵入性更低,servlet代码侵入性就很高又要实现接口或者继承httpServlet。
public class UserAction { }
方式2.
import com.opensymphony.xwork2.Action; //方式2 实现xwork2下的Action接口 不常用
public class HelloAction implements Action{
//接口中有个方法 属于提示方法告诉你怎么写
@Override
public String execute() throws Exception {
return null;
}
}
方式3.
import com.opensymphony.xwork2.ActionSupport; //方式3 继承一个类ActionSupport
public class HelloAction extends ActionSupport{
public String hello(){
System.out.println("hello david");
return "success";
}
}
继承该类的用处:https://blog.csdn.net/foreverdwyw/article/details/53462866
struts2框架搭建(一)的更多相关文章
- Struts2框架搭建
使用Struts2版本为:struts-2.3.16.3 下载地址:http://struts.apache.org/ 一.向web项目中添加struts2依赖库 commons-fileupload ...
- struts2框架搭建学习遇到的问题
遇到的两个问题 1Dispatcher initialization failed: 严重: Dispatcher initialization failed java.lang.RuntimeExc ...
- struts2的搭建和简单的例子(采用struts-2.5.2版本)
struts框架的概述: 当2001年初,Struts的第一个版本在apache网站上发布,它提供了一种分离视图和业务应用逻辑的web应用方案. 在Struts诞生之前,开发人员都是在jsp里写入处理 ...
- SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>
此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...
- 搭建一个简单struts2框架的登陆
第一步:下载struts2对应的jar包,可以到struts官网下载:http://struts.apache.org/download.cgi#struts252 出于学习的目的,可以把整个完整的压 ...
- Struts2 框架的快速搭建
方便myEclipse 手动配置Struts2框架,写下此文,需要的朋友拿走不谢~ 一.引入JAR包 WEB工程->WebRoot->WEB-INF->lib引入Struts2对应版 ...
- 搭建struts2框架
struts是一个经典的MVC模式拦截器比过滤器拦截的力度更大 搭建struts2框架1.引入lib包 9个(2.3版本的)common-fileupload;common-io;common-lan ...
- Spring+Struts2+Mybatis框架搭建时的常见典型问题
搭建SSM框架时,总是遇到这样那样的问题,有的一眼就能看出来,有的需要经验的积累.现将自己搭建SSM框架时遇到的典型问题总结如下: 一.Struts2框架下的action中无法使用@Autowired ...
- 第3章 Struts2框架--1、Struts2环境搭建
第3章 Struts2框架--1.Struts2环境搭建 搭建步骤: 1.从下载http://struts.apache.org 没找到Struts2.3.16版,就下载了2.3.29 2.拷贝后解压 ...
随机推荐
- 解决hibernate删除时的异常 deleted object would be re-saved by cascade (remove deleted object from associa
今天在做项目时,需要删除一个对象,由于关联关系是一对多和多对一的关系,于是在代码中需要删除多的一方的对象时出现了 deleted object would be re-saved by cascade ...
- kernel中的函数指针
经常会看到这类的结构体: 这个结构体中 有几个函数指针, 这种方式的好处,可以有多种具体的函数实现,但是,这样就统一了接口 struct address_space_operations { int ...
- Spring Web MVC常用配置汇总
pom.xml ============================================================================================ ...
- 面向对象:__getitem__、__setitem__、__delitem__
item系列 class Person(object): def __init__(self, name): self.name = name def __getitem__(self, item): ...
- LVM(Logical Volume Manager)逻辑卷管理
本文实验部分,完全由本人亲自动手实践得来 文章中有部分的内容是我个人通过实验测试出来的,虽以目前本人的能力还没发现不通之处,但错误难免,所以若各位朋友发现什么错误,或有疑惑.更好的建议等,盼请各位能在 ...
- 30.IK分词器配置文件讲解以及自定义词库
主要知识点: 知道IK默认的配置文件信息 自定义词库 一.ik配置文件 ik配置文件地址:es/plugins/ik/config目录 IKAnalyzer.cfg.xml:用 ...
- 【模板】Link-Cut Tree
#include<cstdio> #include<algorithm> #define N 500010 #define rg register #define ls (c[ ...
- BZOJ 4032 Luogu P4112 [HEOI2015]最短不公共子串 (DP、后缀自动机)
这其实是道水题... 题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=4032 (luogu)https://www.luog ...
- 公钥基本结构(PKI)的概念
公钥证书 ,通常简称为证书 ,用于在 Internet.Extranet 和 Intranet 上进行身份验证并确保数据交换的安全.证书的颁发者和签署者就是众所周知的 证书颁发机构 (CA),将在下一 ...
- bupt summer training for 16 #6 ——图论
https://vjudge.net/contest/174020 A.100条双向边,每个点最少连2个边 所以最多100个点,点的标号需要离散化 然后要求恰好经过n条路径 快速幂,乘法过程就是flo ...