2、Struts2之HelloWorld程序
来源:互联网 发布:淘宝联盟能自己买吗 编辑:程序博客网 时间:2024/05/16 10:02
都说Struts2来自WebWork,完全可以通过学习WebWork来掌握Struts2。于是就先看《WebWork in action》,大概明白了怎么回事就开始用Struts2写HelloWorld程序。下面是最简单的第一个HelloWorld程序。
我的编程平台是:WindowsXP,JDK6,Tomcat6,Struts2.0.6
一、最简单的HelloWorld
先看看文件目录结构。
WEB-INF
│ web.xml
│
├─classes
│ │ powerwind.xml
│ │ struts.xml
│ │
│ └─powerwind
│ HelloWorld.class
│
├─lib
│ commons-logging-1.1.jar
│ freemarker-2.3.8.jar
│ ognl-2.6.11.jar
│ struts2-core-2.0.6.jar
│ xwork-2.0.1.jar
│
├─pages
│ HelloWorld.jsp
│
└─src
HelloWorld.java
其中index.html文件只有一行代码如下:
web.xml文件也只有这么几行内容。
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
struts.xml文件内容如下:
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<include file="powerwind.xml"/>
</struts>
powerwind.xml是配置action的文件,由于才有一个动作,所以内容也很少。
<package name="powerwind" namespace="/powerwind" extends="struts-default">
<action name="HelloWorld" class="powerwind.HelloWorld">
<result>/WEB-INF/pages/HelloWorld.jsp</result>
</action>
</package>
</struts>
HelloWorld类很简单。
import com.opensymphony.xwork2.Action;
public class HelloWorld implements Action
{
public String execute() throws Exception {
return SUCCESS;
}
}
而HelloWorld.jsp的作用就是输出HelloWorld,所以我只写了这么一句: <h3>HelloWorld!</h3>
至此,完成了最简单的一个struts2的HelloWorld程序,却感觉不到Struts的任何好处,倒是觉得本来只要最后一个HelloWorld.jsp文件就可以做完的事,却偏要做得那么复杂。
二、有交互功能的HelloWorld
其实这个也算是很简单的HelloWorld程序,只稍微加了个输入表单,既没有用到I18N资料国际化,也没有用到“validation”的验证方法。
HelloWorld类变成了一个Bean。
import com.opensymphony.xwork2.Action;
public class HelloWorld implements Action
...{
private String name;
public String execute() throws Exception ...{
if(validate())return SUCCESS;
return INPUT;
}
private boolean validate()
...{
if(name==null||name.length()<1)return false;
return true;
}
public void setName(String name)
...{
this.name=name;
}
public String getName()
...{
return name;
}
}
powerwind.xml配置文件也稍稍作了改动。
<result name="input">/WEB-INF/pages/HelloWorld.jsp</result>
<result>/WEB-INF/pages/success.jsp</result>
</action>
差点忘记了。HelloWorld.jsp现在变成这样了。
<%...@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<s:form action="HelloWorld">
<s:textfield key="name"/>
<s:submit/>
</s:form>
</body>
</html>
而新加入的文件success.jsp只有一行内容:Welcome,<s:property value="name"/>
这就是第二个HelloWorld程序。
今天写了两个简单的程序,对struts2的流程有了初步的认识。在接下来的时间里,慢慢深入学习。暂时不对以上两个程序作具体分析。
- 2、Struts2之HelloWorld程序
- Struts2之HelloWorld程序
- 重温struts2之第一个struts2程序helloworld
- Struts2的HelloWorld程序
- Struts2的HelloWorld程序
- Struts2的HelloWorld程序
- Struts2之HelloWorld
- HelloWorld之Struts2
- struts2之HelloWorld
- struts2学习之HelloWorld
- struts2之helloworld
- Struts2之HelloWorld
- Struts2学习之helloworld
- Struts2入门之HelloWorld
- Struts2(一)-Struts2之HelloWorld
- PhoneGap之HelloWorld程序
- Java之helloworld程序
- struts2复习2 helloworld
- .NET委托:一个C#睡前故事
- vc中把字符串转换成16进制数的函数(如'7f'->0x7f)
- 转移阵地
- 国际支付
- 深入探讨如何在 C# 中使用 Win32 和其他现有库
- 2、Struts2之HelloWorld程序
- ASP.net:Literal控件用法
- SWT可以胜任大部分windows桌面程序的开发
- 美食
- 利用JS实现可以自由拖拽调整宽度的表格
- 十二章 类————初始化const或引用类型要用初始化列表,隐式转换问题等,友元
- 几种免费的界面库及其用法(转)
- eclipse 下获取icons
- 面试算法题