Struts2官方推荐教程
Vaan Nila's Struts 2 Tutorial中
Struts 2 Hello World Tutorial一节是Struts2极好的入门文章,很适合我们这些新人学习。
该文章以一个简单的Hello World应用为便,向我们展示了struts2的魅力。
该例的流程非常简单:用户输入姓名后提交,在新的页面中显示问候语。
使用Struts二编写这个Hello World,需要如下步骤:
- 创建Web工程并添加struts包
- 配置过滤器
- 配置struts.xml
- 编写action和视图
- 布署运行
1. 创建web工程并添加struts包
新建一个web工程Struts2Tutorial,在WEB-INFlib文件夹下加入
- struts2-core-2.1.8.1.jar
- xwork-core-2.1.6.jar
- freemarker-2.3.15.jar
- commons-logging-1.0.4.jar
- commons-fileupload-1.2.1.jar
- ognl-2.7.3.jar
2. 配置过滤器
打开web.xml文件,添加过滤器定义如下:
<filter>
<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>
过滤器FilterDispatcher是Struts2的核心,最常见的用途就是将用户请求转发到不同的Action执行,并返回执行结果给用户。
一般情况下,FilterDispatcher的url-pattern必须匹配所有用户请求。
见:
http://struts.apache.org/2.1.6/struts2-core/apidocs/org/apache/struts2/dispatcher/FilterDispatcher.html
引用
IMPORTANT: this filter must be mapped to all requests. Unless you know exactly what you are doing, always map to this URL pattern: /*
3. 配置struts.xml
struts.xml文件用于配置Action,该文件默认应放置在工程的classpath下面,如src文件夹下,当工程编译后部署后,程序访问的其实是WEB-INF/classes/struts.xml文件。
struts.xml的dtd文件可以在struts2-core-2.1.8.1.jar的根目录下找到。
struts.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="helloworld" class="dive.into.struts2.HelloWorld">
<result name="SUCCESS">/success.jsp</result>
</action>
</package>
</struts>
其中pack标签声明了一个默认的包default,它继承了struts的默认配置struts-default。
action标签建立了一个名为helloworld的Action,我们可以通过/helloworld.action访问它。
result标签定义了一个名SUCCESS的执行结果,它指向/success.jsp页面。
4. 编写action和视图
建立类dive.into.struts2.HelloWorld类,这是一个Action,但事实上你不用继承什么类或者实现什么接口,只需要让它符合以下规则即可:
一个返回值为String类型的public方法execute()
其返回值会用于识别struts.xml该Action下的result标签,从而决定跳转的地址。
一组属性及其setter和getter,如果必要的话
该属性可以在jsp页面的struts标签直接访问,非常方便。
HelloWorld.java
package dive.into.struts2;
public class HelloWorld {
private String userName;
private String message;
public String execute() {
setMessage("Hello, " + getUserName());
return "SUCCESS";
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
index.jsp
<%-- 使用Struts2标签库 --%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World :: Struts2</title>
</head>
<body>
<%-- 创建一个提交地址为名为helloworld的Action的表单 --%>
<s:form action="helloworld">
<%--
创建一个文本域,表单提交后,该文本域的值会自动填充到
helloworld对应的Action对象的userName属性上
--%>
<s:textfield name="userName" label="User Name"/>
<%-- 创建一个提交按钮 --%>
<s:submit/>
</s:form>
</body>
</html>
success.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World :: Struts2</title>
</head>
<body>
<h1>
<%--
该页面由helloworld跳转而来
此标签将会显示helloworld实例的message属性的值。
--%>
<s:property value="message"/>
</h1>
</body>
</html>
5. 布署运行
在tomcat中布置工程并通过
http://localhost:8080/Struts2Tutorial
分享到:
相关推荐
学Struts2从HelloWorld示例开始
最基础的基于struts2框架的hello world 实现,适合小白。
可运行的struts2例子,eclipse工程
一个struts2的helloworld例子加源码
本章完成一个HelloWorld实例开发的演示,目的是使读者可以对struts2有一个整体的认识,熟悉glassfish2的使用,主要讲解了一个简单的Struts2应用的HelloWorld实例,这个例子可以帮助读者快速入门,获得开发Struts2...
Struts2 Helloworld 源代码
struts2框架最基本的代码。如果你想看看struts2最基本的样子,你个应该是你不错的选择。但前提是你得学过jsp/servlet
Struts2 2.5.20HelloWorld程序(里面包含Struts至少需要的jar包)
Struts2的入门小实例helloworld,jar包齐全,可以运行。
Struts2的HelloWorld例子适合Struts2的初学者,例子虽小,但是很有帮助。 保证初学者利大于弊~!
自己做的helloworld 希望可以给你们启发
struts2的HelloWorld的例子, 带JAR和配置文件
Struts框架之helloWorld源码,省略struts jar包,其它都有
Struts2入门HelloWorld,加进tomcat即可运行,jar包都已在lib文件夹下面
以前看着视频学习struts2时候写的练习代码,完整的代码,可以供初学者学习或者参考。
NULL 博文链接:https://mbi325.iteye.com/blog/255056
一步一步详细描述用maven构建struts2的HelloWorld程序