本文主要是介绍(二)struts2.0之helloworld,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.在导入包的最小集合
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.jar
xwork-2.0.4.jar
2.在web.xml中编写过滤器
05. <filter>
06. <filter-name>struts2</filter-name>
07. <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
08. </filter>
09. <filter-mapping>
10. <filter-name>struts2</filter-name>
11. <url-pattern>/*</url-pattern>
12. </filter-mapping>
3.编写action
⑴ 编写属性名
每个属性对应表单域的name属性,生成对应的getter和sertter方法
当表单提交到action时,会自动调用对应name的setter方法,sertter的调用在execute方法之前
当使用<s:property value="name">,会调用name对应的getter方法;
⑵重写execute方法
⑶最好继承ActionSupport类
4.编写struts.xml文件
在src根目录下,编写struts.xml文件。为了方便,可以在struts开发包的根目录下apps中找到struts.xml文件,将其复制过来。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="struts2" extends="struts-default"><action name="msg" class="action.Msg"><result name="success">/msg.jsp</result></action></package>
</struts>
这篇关于(二)struts2.0之helloworld的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!