Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334800
  • 博文数量: 89
  • 博客积分: 5152
  • 博客等级: 大校
  • 技术积分: 1155
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-25 15:12
文章分类

全部博文(89)

文章存档

2012年(1)

2011年(5)

2010年(14)

2009年(69)

我的朋友

分类: Java

2009-11-23 11:13:29

Hello World Application

In this tutorial we will see how to create a simpe Struts 2 Hello World Application. The following files are needed to create a Hello World Application.

  • web.xml
  • struts.xml
  • HelloWorld.java
  • index.jsp
  • success.jsp

The following picture shows the directory structure of the Hello World application.

web.xml

web.xml is used to configure the servlet container properties of the hello world appliation. The filter and the filter-mapping elements are used to setup the Struts 2 FilterDispatcher. The filter is mapped to the URL pattern "/*". This means all the incoming request that targets to the Struts 2 action will be handled by FilterDispatcher class.


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation=" /web-app_2_5.xsd">
    <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>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>


The gateway for our hello world application is index.jsp file. The index.jsp file should be mentioned in web.xml as shown above.

struts.xml

The entry point to the XML declarative architecture is struts.xml file. The struts.xml file contains the following action mapping.

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"">

<struts>
    <package name="default" extends="struts-default">
        <action name="HelloWorld" class="vaannila.HelloWorld">
            <result name="SUCCESS">/success.jsp</result>
        </action>
    </package>
</struts>


index.jsp

The Struts 2 UI tags are simple and powerful. To use the struts tags in the jsp page the following taglib directive should be included.



<%--
    Document : index
    Created on : Feb 28, 2009, 8:22:37 AM
    Author : Meyyappan Muthuraman
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello World</title>
    </head>
    <body>
        <s:form action="HelloWorld" >
            <s:textfield name="userName" label="User Name" />
            <s:submit />
        </s:form>
    </body>
</html>


HelloWorld.java

As you see the HelloWorld class is very simple. It contains two properties one for the user name and the other for displaying the message.



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package vaannila;

/**
 *
 * @author Meyyappan Muthuraman
 */


public class HelloWorld {

    private String message;

    private String userName;
    
    public HelloWorld() {
    }

    public String execute() {
        setMessage("Hello " + getUserName());
        return "SUCCESS";
    }

    /**
     * @return the message
     */

    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */

    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return the userName
     */

    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */

    public void setUserName(String userName) {
        this.userName = userName;
    }

}


In the execute() method of the HelloWorld action we compose the message to be displayed. Note we need not have a seperate form bean like struts 1 to access the form data. We can have a simple java class as action. The action need not extend any class or implement any interface. The only obligation is that you need to have an execute() method which returns a String and has a public scope.


success.jsp

In the success page we display the "Hello User" message using the property tag.


<%--
    Document : success
    Created on : Feb 28, 2009, 8:24:14 AM
    Author : eswar@vaannila.com
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Hello World</title>
    </head>
    <body>
        <h1><s:property value="message" /></h1>
    </body>
</html>


Extract the downloaded files into the webapps folder of Tomcat. Start the Tomcat server. Type the following url in the browser "". The index page will be displayed.






Enter the user name and submit the form. Hello user name message will be displayed.


Source + Lib :
阅读(978) | 评论(0) | 转发(0) |
0

上一篇:Spring MVC

下一篇:Struts 2 UI Tags Example

给主人留下些什么吧!~~