strut2框架搭建
1.导包:
官网下载strut2
strut2开发商并没有将这个jar进行分类,我们导报很难,所以进入APP文件下,这个文件下是一些war包,
这些war包都是demo。将war包当中的包全部复制出来就是我们想要的包,将Struts2_blank.war改成Rar文件
然后解压,然后将web-info下面的lib包全部导入项目中就完成了。
2.创建action类(做参考)
public class HelloAction {
public String hello(){
System.out.println("hello struts2");
return "success";
}
}
3.创建配置文件struts.xml
①文件必须在src路径下 名字也必须是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>
<!--il8n 国际化 相当于我们在servlet中设置的编码
解决了post请求的中文乱码问题-->
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<!--extension 使用设置action访问的扩展名
action
可以改为do 为什么要改成go 以为struts1用的是do-->
<!--<constant name="struts.action.extension" value="do"></constant>-->
<!--developerMode 开发者模式
1.热部署 你修改配置文件后,等一段时间会自动加载
2.提高错误信息的提示(友好的错误提示)-->
<constant name="struts.devMode" value="true"></constant>
<!-- package :他跟项目里面的包没关系,他是用来管理action-->
<!--name:给package起个名字,没什么意义,可以随便命名,一般情况下他的命名是按当前package管理action的分类来命名
package和 package的名字不能重复
namespace:访问路径地址前缀 跟name没有关系 跟其他的package中的 namespace能不能重复,没有要求
extend:继承 继承自struts-default 必须写 名字不能改 固定的写法 他是从struts2核心包中struts-2.2-core下面的
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:对应的是action类中的method的返回值
名字可以随便写只要和action类中的返回值结果一样就可以,但是情况下使用特定的
success:代表成功
type:dispatcher 转发
redirect 重定向
标签中间的值:跳转页面的地址
-->
<result name="success">/hello.html</result>
</action>
</package>
<include file="cn/hd/dynamic/struts01.xml"></include>
<include file="cn/hd/struts2_default_demo/struts02.xml"></include>
</struts>
②去配置action
<?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>
<!--name 包的名字 name extends 默认配置文件-->
<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>
</struts>
③创建一个hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>第一个Struts2</title>
</head>
<body>
hello Struts2
</body>
</html>
④在web.Xml中去配置过滤器。这是因为strusuts2是基于过滤器。
配置过滤器的类名比较长,打开编译器的全局搜索。输入StrutsP 就可以复制这个类的相对路径
<?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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4.测试
在浏览器中输入
页面上显示内容为hello.html中的内容,控制台打印hello struts2
总结:404错误
1. struts.xml 文件位置放错
2. struts.xml 文件名字写错
3. Hello.html文件位置放错了
4. Tomacat启动失败
5. struts.xml 中配置写错了
一个package中的namespace和地址中不一致
Action中的name和地址中的helloAction不一致
Result里面name字段和action中的返回值不一致
Result中的值不是/hello.html写错了