今天开始学习struts2框架的学习,其实原理啥的都看不懂啦,好多程序照着书上敲,最后运行都会出现各种各样莫名奇妙的问题写写一天的收获,希望自己能一直坚持下去。
编写Struts2项目的一般步骤:
1.将Struts2的支持类型库添加到web-inf下的lib文件夹中:
2.配置web.xml文件,只需在在默认的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>*.action</url-pattern>
</filter-mapping>
3.在项目的源文件(src)下配置Struts.xml,如下:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<!-- 声明包 -->
<package name="myPackage" extends="struts-default">
<!-- 定义action -->
<action name="testAction" class="com.lq.action.TestAction" >
<result name="success">success.jsp</result>
</action>
</package>
</struts>照葫芦画瓢啦,其实不知道action中的name属性是怎样命名的
4.然后写你的action类,注意action类必须继承于ActionSupport,不然你的return SUCCESS,return ERROR就会报错,action类中要复制粘贴的一行代码public static final long serialVersionUID = 1L;action类中一般要写execute方法
5.然后写你的index.jsp,success.jsp等等