Maven3+Struts2.3.1.2整合的Hello World例子
来源:互联网 发布:招商银行贵金属软件 编辑:程序博客网 时间:2024/05/20 10:23
这个例子讲解的是Struts2的Hello 例子。
本文的整合的环境是:
1.Maven 3
2.Eclipse 3.7
3.Struts 2.3.1.2
1.项目结构图:
本文的最终结构图如下所示,防止你跟不上后面的步骤
2.添加Struts2依赖
使用Maven需要添加Struts2依赖,在pom.xml文件里需要添加“struts2-core”
pom.xml文件内容如下所示:
<
project
xmlns
=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<
modelVersion
>4.0.0</
modelVersion
>
<
groupId
>com.mkyong.common</
groupId
>
<
artifactId
>Struts2Example</
artifactId
>
<
packaging
>war</
packaging
>
<
version
>com.mkyong.common</
version
>
<
name
>Struts2Example Maven Webapp</
name
>
<
url
>http://maven.apache.org</
url
>
<
dependencies
>
<
dependency
>
<
groupId
>junit</
groupId
>
<
artifactId
>junit</
artifactId
>
<
version
>3.8.1</
version
>
<
scope
>test</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>org.apache.struts</
groupId
>
<
artifactId
>struts2-core</
artifactId
>
<
version
>2.3.1.2</
version
>
</
dependency
>
</
dependencies
>
<
build
>
<
finalName
>Struts2Example</
finalName
>
<
plugins
>
<
plugin
>
<
artifactId
>maven-compiler-plugin</
artifactId
>
<
version
>2.3.2</
version
>
<
configuration
>
<
source
>1.6</
source
>
<
target
>1.6</
target
>
</
configuration
>
</
plugin
>
</
plugins
>
</
build
>
</
project
>
3.转换到Eclipse项目
编译并转换到Eclipse Web项目需要执行如下命令提示符:
mvn eclipse:eclipse -Dwtpversion=2.0
复查Eclipse的classpath文件,下面的Struts2的依赖关系被下载:
<
classpath
>
<
classpathentry
kind
=
"src"
path
=
"src/main/java"
including
=
"**/*.java"
/>
<
classpathentry
kind
=
"src"
path
=
"src/main/resources"
excluding
=
"**/*.java"
/>
<
classpathentry
kind
=
"output"
path
=
"target/classes"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/asm/asm/3.3/asm-3.3.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/asm/asm-commons/3.3/asm-commons-3.3.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/asm/asm-tree/3.3/asm-tree-3.3.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/commons-fileupload/commons-fileupload/1.2.2/commons-fileupload-1.2.2.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/commons-io/commons-io/2.0.1/commons-io-2.0.1.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/commons-lang/commons-lang/2.5/commons-lang-2.5.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/org/freemarker/freemarker/2.3.18/freemarker-2.3.18.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/javassist/javassist/3.11.0.GA/javassist-3.11.0.GA.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/ognl/ognl/3.0.4/ognl-3.0.4.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/org/apache/struts/struts2-core/2.3.1.2/struts2-core-2.3.1.2.jar"
/>
<
classpathentry
kind
=
"lib"
path
=
"C:/Program Files/Java/jdk1.6.0_13/lib/tools.jar"
/>
<
classpathentry
kind
=
"var"
path
=
"M2_REPO/org/apache/struts/xwork/xwork-core/2.3.1.2/xwork-core-2.3.1.2.jar"
/>
<
classpathentry
kind
=
"con"
path
=
"org.eclipse.jdt.launching.JRE_CONTAINER"
/>
</
classpath
>
4.JSP页面
下面的登陆页面用的是Struts2标签,里面包含一个用户名,密码和一个提交按钮
login.jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<
html
>
<
head
></
head
>
<
body
>
<
h1
>Struts 2 Hello World Example</
h1
>
<
s:form
action
=
"Welcome"
>
<
s:textfield
name
=
"username"
label
=
"Username"
/>
<
s:password
name
=
"password"
label
=
"Password"
/>
<
s:submit
/>
</
s:form
>
</
body
>
</
html
>
welcome_user.jsp ,这个页面是给用户展示欢迎信息
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<
html
>
<
head
></
head
>
<
body
>
<
h1
>Struts 2 Hello World Example</
h1
>
<
h4
>
Hello
<
s:property
value
=
"username"
/>
</
h4
>
</
body
>
</
html
>
Struts1和Struts2的标签语法非常相似,仅仅是标签的命名上有点小区别,例如:
Struts1如下所示:
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<
html:form
action
=
"Welcome"
>
<
html:text
property
=
"username"
/>
</
html:form
>
Struts2如下所示:
<%@ taglib prefix="s" uri="/struts-tags" %>
<
s:form
action
=
"Welcome"
>
<
s:textfield
name
=
"username"
label
=
"Username"
/>
5.Action-把所有的业务逻辑放在这儿
这是一个简单的Struts2 Action类,这里用来处理所有的业务逻辑。
WelcomeUserAction.java
package
com.mkyong.user.action;
public
class
WelcomeUserAction{
private
String username;
public
String getUsername() {
return
username;
}
public
void
setUsername(String username) {
this
.username = username;
}
// all struts logic here
public
String execute() {
return
"SUCCESS"
;
}
}
在Struts2中,需要实现任何接口或者任何父类,但是它需要创建一个execute()方法用来处理所有的业务逻辑并返回一个String类型的值用来告诉用户跳转到哪儿去。
注意1:你也许看到有些开发者实现了com.opensymphony.xwork2.Action类,但是它不是必须需要实现的类,因为这个类只是提供了几个常量而已
注意2:Struts1是必须实现com.opensymphony.xwork2.Action类的,但是Struts2是可选的,你也可以实现com.opensymphony.xwork2.Action类去使用一些常量或者你可以继承com.opensymphony.xwork2.ActionSupport去使用某些默认的通用的值
5.Struts2配置文件
Struts2的配置文件用来配置把所有的东西组装在一起,这个xml文件名必须为“struts.xml”
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
=
"user"
namespace
=
"/User"
extends
=
"struts-default"
>
<
action
name
=
"Login"
>
<
result
>pages/login.jsp</
result
>
</
action
>
<
action
name
=
"Welcome"
class
=
"com.mkyong.user.action.WelcomeUserAction"
>
<
result
name
=
"SUCCESS"
>pages/welcome_user.jsp</
result
>
</
action
>
</
package
>
</
struts
>
这里面配置了包名和Action类名,Action类名你也许很熟悉,但你也许会对下面的新标签感兴趣:
1.包名name=”user”
只是一个包名而已,不用太在意
2.命名空间namespace=”/User”
它用来匹配“/User”路径
3.继承包名 extends=”struts-default”
它的意思是集成struts-default的组件和拦截器,它声明在struts-default.xml文件里,这个文件存放在struts2-core.jar 文件里
6.Web.xml文件
在你的项目的web.xml文件里配置Struts2
web.xml
!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<
web-app
>
<
display-name
>Struts 2 Web Application</
display-name
>
<
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
>
</
web-app
>
7.运行
在struts2里,访问这个Action类你可以添加“.action”扩展名
ttp://localhost:8080/Struts2Example/User/Login.action
http://localhost:8080/Struts2Example/User/Welcome.action
原创文章,转载请注明出处:http://www.it161.com/article/javaDetail?articleid=140112224129
更多原创内容,请访问:http://www.it161.com/
- Maven3+Struts2.3.1.2整合的Hello World例子
- Struts2的hello world教程
- 10002---Struts2 的 Hello World
- Struts2权威指南总结2:Struts2的Hello World
- struts 2—搭建struts2下的Hello world
- BIEE Hello World式的例子
- node.js 分析hello world的例子
- RabbitMQ手册翻译 - Hello World的例子
- spring mvc Hello World 的例子
- Neon加速的Hello world例子
- 一个简单的例子Docker Hello World
- Struts2 开发一个简单的Hello World
- qt4 hello world 例子
- libevent例子:hello-world
- struts2学习笔记2-strut2 hello world
- STRUTS 2-EXAMPLES:Hello World Struts2 project.
- Struts2 Hello World实例
- Struts2 Hello World 实例
- bootchart工具在Android系统开机测量中的应用
- HDU 1060(数论)
- 数据库中创建视图和索引还有存储过程起什么作用?
- CSS背景属性(1)
- Windows CE 6.0 安装顺序
- Maven3+Struts2.3.1.2整合的Hello World例子
- 删除UserDefault里某个object致app crash的解决过程
- sql练习 根据网上的习题,自己又做了个数据库,练习
- log4jdbc日志框架
- 1 android下的数据持久化,保存数据到rom文件
- javascript 数组详解 Array
- iOS7下status bar相关问题的解决方法
- VxWorks增加组件
- Leetcode: Anagrams