开发步骤:
1、导入8个JAR包(放在Dynamic web项目的lib下)
2、配置web.xml文件,添加struts2的过滤器
3、创建JSP视图页面
4、创建action的java类
5、配置struts.xml文件,设置视图关联
搭建步骤:
1、导入需要的8个包,jar包来源:http://pan.baidu.com/s/1o7IgiBC
2、配置web.xml文件
<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>
3、在SRC目录下面创建STUCTS.XML文件,XML内容可以从官方的DEMO中获取
STUCTS.XML文件中的内容:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="demo" namespace="" extends="struts-default">
<action name="hello" method="excute" class="action.HelloAction">
<result name="ok">/index.jsp</result>
</action>
</package>
</struts>
说明:
package节点类似JAVA中的包,STRUTS2引用包的概念,name属性就是包名,可以根据业务或功能分类定义不同的包,避免命名冲突,namespace类似于命名空间,决定内部所有的ACTION访问URL的父级地址,如(namespace="/demo/page",以下访问的路径就需要加上“http://localhost:8080/项目名/demo/page/”), extends属性必须写上,而且值必须是“struts-default”,这样才能够实现struts2的功能。
package节点内的action节点可以有多个,表示同个包中多个动作指向URL;
action节点中的name属性是指定访问时的URL文件名,如上所示的的完整URL是:http://localhost:8080/项目名/demo/page/hello, class属性是指定当前URL所指向的action的完整类名,如果不指定值,则默认是XWORK包中的ActionSupport类,method属性是指向action类中的执行方法,该方法可以自定义,但返回值必须是字符串,如果不指定值,默认是:excute方法。
result节点中的name属性值是指定action类中的method所指定的方法返回的字符串值,节点中的文本是指向当接收到name的值时转发的URL路径。
result节点中有type属性,用于指定指向URL路径的方式,主要值有:dispatcher(默认值),redirect,redirectAction,plainText等,
其中当使用redirectAction值时,是直接写重定向到其他action,如果是指向当前包下面的其他ACTION,则中间节点直接写ACTION名称即可,如果是指向其他包中的ACTION,则需要添加PARAM节点,如:
<param name="actionName">其他ACTION的名称</param>
<param name="namespace">ACTION所在的命名空间</param>
其中当使用plainText值时,一般是应用于将某个页面的源代码直接输出到页面,如:
<result type="plainText">
<param name="location">/index.jsp</param>
<param name="charset">utf-8</param>
</result>
1、导入8个JAR包(放在Dynamic web项目的lib下)
2、配置web.xml文件,添加struts2的过滤器
3、创建JSP视图页面
4、创建action的java类
5、配置struts.xml文件,设置视图关联
搭建步骤:
1、导入需要的8个包,jar包来源:http://pan.baidu.com/s/1o7IgiBC
2、配置web.xml文件
<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>
3、在SRC目录下面创建STUCTS.XML文件,XML内容可以从官方的DEMO中获取
STUCTS.XML文件中的内容:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="demo" namespace="" extends="struts-default">
<action name="hello" method="excute" class="action.HelloAction">
<result name="ok">/index.jsp</result>
</action>
</package>
</struts>
说明:
package节点类似JAVA中的包,STRUTS2引用包的概念,name属性就是包名,可以根据业务或功能分类定义不同的包,避免命名冲突,namespace类似于命名空间,决定内部所有的ACTION访问URL的父级地址,如(namespace="/demo/page",以下访问的路径就需要加上“http://localhost:8080/项目名/demo/page/”), extends属性必须写上,而且值必须是“struts-default”,这样才能够实现struts2的功能。
package节点内的action节点可以有多个,表示同个包中多个动作指向URL;
action节点中的name属性是指定访问时的URL文件名,如上所示的的完整URL是:http://localhost:8080/项目名/demo/page/hello, class属性是指定当前URL所指向的action的完整类名,如果不指定值,则默认是XWORK包中的ActionSupport类,method属性是指向action类中的执行方法,该方法可以自定义,但返回值必须是字符串,如果不指定值,默认是:excute方法。
result节点中的name属性值是指定action类中的method所指定的方法返回的字符串值,节点中的文本是指向当接收到name的值时转发的URL路径。
result节点中有type属性,用于指定指向URL路径的方式,主要值有:dispatcher(默认值),redirect,redirectAction,plainText等,
其中当使用redirectAction值时,是直接写重定向到其他action,如果是指向当前包下面的其他ACTION,则中间节点直接写ACTION名称即可,如果是指向其他包中的ACTION,则需要添加PARAM节点,如:
<param name="actionName">其他ACTION的名称</param>
<param name="namespace">ACTION所在的命名空间</param>
其中当使用plainText值时,一般是应用于将某个页面的源代码直接输出到页面,如:
<result type="plainText">
<param name="location">/index.jsp</param>
<param name="charset">utf-8</param>
</result>