一、Hello World
开始第一个程序 "Hello World",你需要做三件事:
  1. 建一个网页
  2. 建立一个处理信息的 Action 类
  3. 建立一个连接Action与网页之间的映射(Maping)
网页:(放在工程根目录)
HelloWorld.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
    <head>
        <title>Hello World!</title>
    </head>
    <body>
        <h2><s:property value="message" /></h2>
    </body>
</html>
HelloWorld类:
 
package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {

    public static final String MESSAGE = "Struts is up and running ...";

    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;
    }
}
 
修改struts.xml文件:
 
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="tutorial" extends="struts-default">
        <action name="HelloWorld" class="tutorial.HelloWorld">
            <result>/HelloWorld.jsp </result>
        </action>
        <!-- Add your actions here -->
    </package>
</struts>
 
好了,在你的浏览器中进行访问: