struts2框架是用来替代servlet和jsp。 它的功能就是处理访问服务器的请求
1.接收参数(自动封装参数)
2.参数的校验
3.可以控制页面的跳转
4.防止表单数据重复提交
5.显示等待页面
官网下载Struts2
Struts2开发商并没有将这个jar进行分类,所以我们导包很难,所以进入到app文件下,这个文件下是一些war包,这些war都是demo。将war包当中的包全部复制出来就是我们要用的包。将struts2_blank.war改成.rar文件然后解压,
然后将WEB-INFO下面的lib包全部导入到项目中。就完成了导包。
public class HelloAction {
public String hello(){
System.out.println ("hello struts2");
return "success";
}
}
(1)文件位置,必须在src目录下 名字也必须是struts.xml导入约束
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" " http://struts.apache.org/dtds/struts-2.3.dtd">
然后再引入struts标签。
(2)去配置action
<package name="hello" namespace="/hello" extends="struts-default">
<action name="helloAction" class="cn.hd.hello.HelloAction" method="hello">
<result name="success">/hello.html</result>
</action>
</package>
(3)创建一个hello.html
(4)在web.xml中去配置过滤器。这是因为struts2是基于过滤器。
配置过滤器的类名比较长,打开编译器的全局搜索,输入StrutsP就可以了赋值这个类的相对路径
( 5)测试
在浏览器中输入http://localhost:8080/hello/helloAction
页面上显示内容为hello.html内容,控制台打印hello struts2内容
总结:404错误
1.struts.xml 文件位置放错了
2..struts.xml 文件名写错了(是struts.xml 不是struts2.xml)
3.hello.html文件位置放错了
4.tomcat启动失败---
5.struts.xml中配置写错了
namespace对应的地址
<!--
package:用来管理action (和项目中的包没关系)
name :给这个package起个名字 没有意义可以随便命名,一般情况下他的命 名是按照当前package管理action的分类来命名(可以有多个package 但是名字不能重复)
namespace:访问路径地址前缀 跟name无关
和其他的package中的namespace能不能重复,没有要求
extends:继承 继承自struts-default 必须写 名字不能改
从struts2核心包中 struts2-core-2.3.24下面的
struts-default.xml这里面配置了很多默认属性
abstract:抽象 声明的一个标志,当前这个配置文件不能独立
运行,等待被继承
-->
<package name="hello" namespace="/hello" extends="struts-default">
<!--
action配置详解
name:给action起名字,决定了访问路径最后的地址
class:类的完整路径名
method:访问类中的方法
-->
<action name="helloAction" class="cn.hd.hello.HelloAction" method="hello">
<!--result
name:对应的是action类中的method的返回值
名字可以随便 只要和action类中的返回值结果一样
但是一般情况下使用特定
type:dispatcher 转发 redirect 重定向
标签中间的值:跳转页面的地址
-->
<result name="success">/hello.html</result>
(4)include在src srtuts.xml可以读取其他位置的xml配置文件 要求新创建的xml必须也有约束
<include file="cn/hd/dynamic/struts01.xml"></include>