1.在web.xml中配置Struts2
<filter>
<filter-name>strust2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>strust2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.在src创建struts.xml,struts-sys.xm,struts-base.xml文件三个文件(名字按自己易意愿取)
特别注意文件的位置 一定要在src下面!!!!!!说到这里真的想抽我自己啊,唉~
--------------------------------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>
<include file="struts-default.xml"></include>
<include file="struts-base.xml"></include>
<include file="struts-sys.xml"></include>
</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.devMode" value="true"></constant>
<constant name="struts.configuration.xml.reload" value="true"></constant>
<package name="base" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
</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>
//namespace属性可以不写,如果写了,在跳转action之前就要见前面加上
//以我的为例子 --> /zking/testAction.action
<package name="zking" namespace="/zking" extends="base">
<action name="testAction" class="com.zking.action.TestAction"></action>
</package>
</struts>
========================================================================
在这里要注意,这里的xml文件在引入dtd约束时不一定会有提示,这个时候就需要将对应的dtd引入:
2.1.点击在菜单栏中window
2.2.选择preferences
2.3.点击add按钮
2.4.其中我的Key的值为"http://struts.apache.org/dtds/struts-2.5.dtd"
=======================好,然后我们回来-------------------------------------------
5.最后写Action,在里面写一个默认的execute方法system.out.print();输出测试
6.进入浏览器测试
笔记:
1.耦合
利用ServletActionContext获取作用域
获取四大作用域:
//非注入
ServletActionContext.getRequest();//获取request
ServletActionContext.getResponse();//获取response
ServletActionContext.getContext().getSession();//获取session
ServletActionContext.getContext().getApplication();//获取application
//注入(要实现ServletActionAware实现ServletRequestAware接口) 重写方法
//以response为例
//私有化response
private HttpServletResponse resp;
@Override
public void setServletResponse(HttpServletResponse resp) {
// TODO Auto-generated method stub
this.resp=resp;
}
2.用ActionContext解耦
ActionContext.getContext().getSession();//获取session对象
ActionContext.getContext().getApplication();//获取application对象
ActionContext.getContext().get("request");