Struts2's HelloWorld

来源:互联网 发布:不惑 知天命 编辑:程序博客网 时间:2024/05/18 18:16

step 1 :

             Go http://struts.apache.org/2.x/, and download thestruts-2.2.3.1-all.zip . Then extract the file, you can find the lib directory.

step 2:

             Config the Environment var, CLASSPATH, add the path to the struts2's lib directory.

step 3:

             Build a new webapp for helloworld:

             HelloWorld/

                                WEB-INF/classes/

                                WEB-INF/classes/struts.xml

                                WEB-INF/lib/

                                WEB-INF/web.xml

                                JSP and HTML files.

              a. Build a new directory named "HelloWorld".

              b. Build a new directory named "WEB-INF" in the direcotry "HelloWorld".

              c.Build a new directory named "classes" in the directory "WEB-INF".

              d.Build a new directory named "lib" in the directory "WEB-INF", and then put the jar files from struts2's lib directory.

              e. Build a new file,put it to the directory "HellowWorld", which is the webapp root's directory , named "helloworld.jsp", contents as follow:             

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Hello world</title></head><body><h2><s:property value="message" /></h2></body></html>

f.Build a new file in the "WEB-INF" directory, named "web.xml", the content as follow:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</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>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

g.Build a file named "struts.xml", and then put it in the "classes" directory. The content as follow:

<?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="helloworld" extends="struts-default">
        <action name="HelloWorldAction" class="helloworld.HelloWorldAction">
            <result>/helloworld.jsp</result>
        </action>
    </package>
</struts>

h.Build a file named “HelloWorldAction.java”, and then put it to the "classes" root directory, and compil it.Content as follow:

package helloworld;
import com.opensymphony.xwork2.*;
public class HelloWorldAction extends ActionSupport
{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    public static final String MESSAGE = "Hello World! , I'm from struts2";
    public String execute() throws Exception
    {
        setMessage(MESSAGE);
        return SUCCESS;
    }
    private String message;
    public void setMessage(String message)
    {
        this.message = message;
    }
    public String getMessage()
    {
        return message;
    }
}

step 4.

           put the "HelloWorld" webapp to the Tomcat's webapps directory. Then restart your tomcat.

step 5.

           type the url :"http://localhost:8080/HelloWorld/HelloWorldAction.action". if you see the picture as follow:



Congratulations , you have your first hello world with struts2.

Otherwise , you should take more carefully to read the steps.

Come on !

原创粉丝点击