struts2学习笔记

    技术2022-05-11  124

    <script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

    www.javabc.com

    struts2学习笔记

    struts2,今天在apache网站上无意中看到了struts项目2.0.1正式发布了,怀着欣喜的心情我下载了stuts2.0.1完整包。Struts2.0.1是struts项目和WebWork2.2项目的合并版本,集成了两大流行MVC框架的优点,对struts框架来说是一个大的提升,同时也更大程度地简化了开发人员的开发过程。我简单地研究了一下这个新版本,现在给大家介绍一个入门级的小例子,希望能对学习这个新版本的朋友有点帮助。这个例子完成了一次URL调用返回结果页面的过程。首先,要从apache网站上下载struts2.0.1的完整包(可以从这里下载:http://struts.apache.org/downloads.html ),解压后需要找到下列几个文件:commons-logging-1.0.4.jarfreemarker-2.3.4.jarognl-2.6.7.jarstruts2-api-2.0.1.jarstruts2-core-2.0.1.jarxwork-2.0-beta-1.jar然后,我们就开始做第一个例子,我们就使用经典的“HelloWorld”的名字吧!1. 制作目录结构如下图所示。stuts2是web应用的根目录。2. 拷贝引用文件将上面列举的jar文件拷贝到步骤1中制作的目录struts2/WEB-INF/lib中。3. 制作jsp文件HelloWorld.jsp<%@ taglib prefix="s" uri="/struts-tags" %><html><head><title>Hello World!</title></head><body><h2><s:property value="message" /></h2></body></html>将该文件拷贝到步骤1中制作的目录struts2/example中。4.制作java文件HelloWorld.javapackage example;/*** <code>Set welcome message.</code>*/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;}}使用下面的命令编译这个java文件:set CLASSPATH=yourdirectory/xwork-2.0-beta-1.jarjavac HelloWorld.java将编译后的HelloWorld.class文件拷贝到步骤1中制作的目录struts2/WEB-INF/classes/example中。5.制作web应用的描述文件web.xml<?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.FilterDispatcher</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>将该文件拷贝到步骤1中制作的目录struts2/WEB-INF中。6.制作MANIFEST.MF文件(从其它地方随便找一个即可)将该文件拷贝到步骤1中制作的目录struts2/META-INF中。7.制作struts配置文件struts.xml和struts.propertiesStruts.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="abc" namespace="/example" extends="struts-default"><action name="HelloWorld" class="example.HelloWorld"><result>/example/HelloWorld.jsp</result></action><!-- Add your actions here --></package></struts>Struts.properties文件:struts.devMode = truestruts.enable.DynamicMethodInvocation = false将这两个文件拷贝到步骤1中制作的目录struts2/ WEB-INF/classes中。好了整个例子的文件我们都搞定了。最后,你可以将制作好的web应用struts2拷贝到tomcat下运行。访问:http://localhost:8080/struts2/example/HelloWorld.action如果能够看到页面上的“Struts is up and running ...”提示信息说明你的例子是正确的。struts2.0后续 <script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>


    最新回复(0)