<package name="tutorial" extends="struts-default">的name属性是什么
当提交一个Html的Form给Struts2框架时,数据不再是提交给服务器端的某一个JSP页面,而是提交给一个Action类。而框架根据配置文件把与该Action类对应的页面(这个页面可以是JSP页面,也可以是PDF、Excel或Applet)返回给客户端。
写一个Struts2的HelloWorld , 我们需要做三件事:
1. 创建一个显示信息的JSP文件
2. 创建一个生成信息的Action类
3. 建立JSP页面和Action的mapping(映射)
创建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.jsp存放在war目录下面
创建Action类HelloWorld.java
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;
}
}
HelloWorld.java存放在src/tutorial下面
在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>
此文件存放在classes下面,同时还要建一个struts.properties的属性文件放在这个目录下,这个文件可以是空的,什么都不写
创建web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>Struts2 Hello World!</display-name>
<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>
</web-app>
web.xml毫无疑问放在WEB-INF下
创建build.xml
<?xml version="1.0"?>
<project name="struts2app" basedir="." default="usage">
<property file="build.properties"/>
<property name="src.dir" value="src"/>
<property name="web.dir" value="war"/>
<property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
<property name="name" value="struts2app"/>
<path id="master-classpath">
<fileset dir="${web.dir}/WEB-INF/lib">
<include name="*.jar"/>
</fileset>
<!-- We need the servlet API classes: -->
<!-- for Tomcat 4.1 use servlet.jar -->
<!-- for Tomcat 5.0 use servlet-api.jar -->
<!-- for Other app server - check the docs -->
<fileset dir="${appserver.home}/common/lib">
<include name="servlet*.jar"/>
</fileset>
<pathelement path="${build.dir}"/>
</path>
<target name="usage">
<echo message=""/>
<echo message="${name} build file"/>
<echo message="-----------------------------------"/>
<echo message=""/>
<echo message="Available targets are:"/>
<echo message=""/>
<echo message="build --> Build the application"/>
<echo message="deploy --> Deploy application as directory"/>
<echo message=""/>
</target>
<target name="build" description="Compile main source tree java files">
<mkdir dir="${build.dir}"/>
<javac destdir="${build.dir}" target="1.3" debug="true"
deprecation="false" optimize="false" failonerror="true">
<src path="${src.dir}"/>
<classpath refid="master-classpath"/>
</javac>
</target>
<target name="deploy" depends="build" description="Deploy application">
<copy todir="${deploy.path}/${name}" preservelastmodified="true">
<fileset dir="${web.dir}">
<include name="**/*.*"/>
</fileset>
</copy>
</target>
</project>
build.xml放在struts2app目录下,再在这个目录下建一个build.properties文件,内容如下:
按照build.properties配置你的tomcat位置。在struts2app目录下运行ant build ,ant deploy ,程序就发布到tomcat的webapps下
# Ant properties for building the springapp
appserver.home=d:/tomcat5.5
deploy.path=${appserver.home}/webapps
tomcat.manager.url=http://localhost:8080/manager
tomcat.manager.username=admin
tomcat.manager.password=admin
运行
现在,启动tomcat,访问http://localhost:8080/tutorial/HelloWorld.action,能看到页面的title为"Hello World!" ,页面上显示"Struts is up and running!".
它们怎么运行的
1、 struts2容器收到HelloWorld.action请求,从web.xml获取设置,org.apache.struts2.dispatcher.FilterDispatcher是所有应用(包括*.action)的入口点。
2、 struts2在struts.xml中找到HelloWorld类(Action),并调用它的execute方法。
3、 execute方法给message变量赋值,并返回SUCCESS,struts2收到SUCCESS标志,按照映射关系,把HelloWorld.jsp返回客户端。
4、 当HelloWorld.jsp开始运行,<s:property value="message" />会调用HelloWorld类getMessage方法,把结果显示在页面上。
分享到:
相关推荐
<package name="default" extends="tiles-default"> <action name="go" class="com.action.MyAction"> <!--result name="success">/next.jsp</result--> <result name="success" type="tiles">myapp.homepage</...
<package name="default" namespace="/" extends="struts-default"> <action name="Logon" class="com.cleversoft.struts2.demo.Logon" method="{1}"> <result name="input">/WEB-INF/jsp/Logon.jsp</result> ...
<package name="default" extends="struts-default"> <interceptors> <interceptor name="timer" class=".."/> <interceptor name="logger" class=".."/> </interceptors> ... </package> ``` 这里的`...
<package name="default" namespace="/" extends="struts-default"> <action name="welcome" class="com.example.WelcomeAction"> <result type="tiles">main</result> </action> </package> </struts> ``` ...
<package name="package1" extends="struts-default"> <action name="Hello" class="tutorial.HelloWorld"> <result name="aaa">HelloWorld.jsp</result> </action> <action name="HelloWorld" class="package1...
<package name="default" extends="struts-default" namespace="/"> <interceptors> <interceptor-stack name="defaultStack"> <interceptor-ref name="fileUpload"> <param name="maxSize">104857600</param> ...
<package name="default" extends="struts-default"> <interceptors> <interceptor name="timer" class="..."/> <interceptor name="logger" class="..."/> <interceptor-stack name="myStack"> <interceptor-...
<package name="tutorial" extends="struts-default"> <action name="checkaction" class="tutorial.CheckAction"> <result name="input">check.jsp</result> <result name="success">success.jsp</result> </...
<package name="default" namespace="/" extends="struts-default"> <action name="fileUpload" class="tutorial.FileUploadAction"> <interceptor-ref name="defaultStack"/> <interceptor-ref name=...
<package name="default" namespace="/" extends="struts-default"> <action name="list" class="tutorial.action.BookAction" method="listBooks"> <result name="list">/list.jsp</result> </action> <action...
<package name="tutorial" extends="struts-default"> <action name="HelloWorld" class="tutorial.HelloWorld"> <result>/HelloWorld.jsp</result> </action> </package> </struts> ``` #### 三、Struts2...
<package name="default" namespace="/" extends="struts-default"> <action name="helloWorld" class="com.example.HelloWorldAction"> <result name="success">/HelloWorld.jsp</result> </action> </package> ...
<package name="default" namespace="/" extends="struts-default"> <action name="example" class="com.example.MyAction"> <result name="success">/success.jsp</result> </action> </package> ``` 这表明有一...
<package name="default" namespace="/" extends="struts-default"> <action name="fileUpload" class="tutorial.FileUploadAction" method="execute"> <result>/success.jsp</result> </action> </package> `...
<package name="ActionDemo" extends="struts-default"> <action name="HelloWorld" class="tutorial.HelloWorld"> <result>/HelloWorld.jsp</result> </action> </package> ``` 这里的配置指定了`HelloWorld`...
2. **包定义**: `<package name="tutorial" extends="struts-default">`创建了一个名为tutorial的包,该包继承自默认的struts-default包。 3. **Action配置**: `<action name="up" class="tutorial.MyUpAction">`...
<package name="book" namespace="/" extends="struts-default"> <action name="create" class="tutorial.action.BookAction" method="create"> <result name="success">/success.jsp</result> </action> <!-- ...