Struts 2 Hello World Example

In this example, we show you how to create a hello world example in Struts 2.

The following libraries or tools are used :

  • Maven 3
  • Eclipse 3.7
  • Struts 2.3.1.2

1. Final project structure

Let review the final project structure of this tutorial, in case you get lost in later steps.

struts2 foder structure

2. Struts2 dependencies

Use Maven to download the entire Struts2 dependencies. Add “struts2-core” in pom.xml.

File : 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. Convert to Eclipse project
Compile and convert that to Eclipse web project in command prompt :

mvn eclipse:eclipse -Dwtpversion=2.0

Review the Eclipse .classpath file, the following Struts2 dependencies are downloaded :

File : .classpath

<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 view pages

A JSP login page to use the Struts 2 tags to display username and password input fields and submit button.

Fie : 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>

File : ·welcome_user.jsp· – A JSP view page to display a welcome message to user.

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head></head>
<body>
    <h1>Struts 2 Hello World Example</h1>

    <h2>
        Hello
        <s:property value="username" />
    </h2>

</body>
</html>

Both Struts 1 and Struts 2 has very similar UI tags syntax, just a little different in term of naming the HTML elements, for example :

Struts 1

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html:form action="Welcome">
   <html:text property="username"/>
</html:form>

Struts 2

<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form action="Welcome">
    <s:textfield name="username" label="Username"/>
</s:form>

5. Action, put all business logic here

A simple Struts2 Action class, it’s used to declared all the business logic inside.

File : 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";

    }
}

In Struts2, the Action class is not required to implement any interface or extend any class, but it’s required to create an execute() method to put all the business logic inside and return a String value to tell user where to redirect.

Note
You may see some users implement the com.opensymphony.xwork2.Action class, but it’s totally optional, because the com.opensymphony.xwork2.Action is just provide some handy constant values only.
Note
Struts1’s Action class is required to extends the org.apache.struts.action.Action. But Struts 2 Action class is optional, but you are still allow to implement the com.opensymphony.xwork2.Action for some handy constant values or extends the com.opensymphony.xwork2.ActionSupport for some common default Action implementation functions.

5. Struts configuration file

A Strut configuration file to link all stuff together. The xml file name must be “struts.xml”.

File : 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>

Declare a package and warp the action classes inside, the action classes are self-explanatory, but you may interest at following new tag :

  1. package name=”user”
    Just a package name, don’t really care about it.

  2. namespace=”/User”
    It’s used to match the “/User” URL pattern.
    很难想象namespace竟然也控制文件的查找路径。

    Note
    Actually, the Struts2 Namespaces is equivalent to Struts 1 multiple modules

  3. extends=”struts-default”
    It means the package is extends the struts-default package components and interceptors, which is declared in the struts-default.xml file, located at the root of the struts2-core.jar file.

6. web.xml

Configure the Web Application Deployment Descriptor (web.xml) file to integrate Struts2 to your web project.

File 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. Run it

In Struts2, you can access the action class directly with a suffix of .action.

http://localhost:8080/Struts2Example/User/Login.action

struts2 hello world example1
http://localhost:8080/Struts2Example/User/Welcome.action

struts2 hello world example2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 JavaScript 编写的记忆游戏(附源代码)   项目:JavaScript 记忆游戏(附源代码) 记忆检查游戏是一个使用 HTML5、CSS 和 JavaScript 开发的简单项目。这个游戏是关于测试你的短期 记忆技能。玩这个游戏 时,一系列图像会出现在一个盒子形状的区域中 。玩家必须找到两个相同的图像并单击它们以使它们消失。 如何运行游戏? 记忆游戏项目仅包含 HTML、CSS 和 JavaScript。谈到此游戏的功能,用户必须单击两个相同的图像才能使它们消失。 点击卡片或按下键盘键,通过 2 乘 2 旋转来重建鸟儿对,并发现隐藏在下面的图像! 如果翻开的牌面相同(一对),您就赢了,并且该对牌将从游戏中消失! 否则,卡片会自动翻面朝下,您需要重新尝试! 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox, 以获得更好、更优化的游戏体验。要玩游戏,首先,通过单击 memorygame-index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值