文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>NetBeans6.8 Struts2.2 HelloWorld

NetBeans6.8 Struts2.2 HelloWorld

时间:2010-09-10  来源:Roam

初学 JAVA 入门,记录下来

1、下载struts http://struts.apache.org/

NetBeans新建项目

2、工具>> 库 >> 新建库

3、左侧项目 >> 库 >> 添加库 选择刚才添加的STRUTS2库

4、双击左侧 WEB.xml 编辑

新建过滤器

过滤器类为 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

 

新建过滤器映射

 

最终WEB.XML内容为

 

代码 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <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>*.do</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

 

 

 

5、左侧项目 >> 源包 >> 缺省包 右键选择新建XML文档

--- struts.xml

 

代码 <?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>
    <constant name="struts.action.extension" value="do" />
    <include file="example.xml"/>
    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>
</struts>

 

--- example.xml

 

代码 <?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="example" namespace="/example" extends="struts-default">
        <action name="HelloWorld" class="example.HelloWorld">
            <result>/example/HelloWorld.jsp</result>
        </action>
        <action name="*" class="example.ExampleSupport">
            <result>/example/{1}.jsp</result>
        </action>
    </package>
</struts>

 

 

 6、创建java类

---- ExampleSupport类 所属包 example

 

package example;
import com.opensymphony.xwork2.ActionSupport;
public class ExampleSupport extends ActionSupport {
}

 

 

---- HelloWorld 类 所属包 example

 

代码 package example;
public class HelloWorld extends ExampleSupport {

    public static final String MESSAGE = "HelloWorld";
    
    public String execute() throws Exception {
        setMessage(MESSAGE);
        return SUCCESS;
    }

    private String message;

    public String getMessage() {
        return message;
    }

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

 

 

7、创建网页文件

--- index.html

 

代码 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
      <a href="example/HelloWorld.do">HelloWorld</a>
  </body>
</html>

 

 

新建example文件夹,在example文件夹下新建HelloWorld.jsp文件

 

代码 <%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>HelloWorld</title>
</head>

<body>
<h2><s:property value="message"/></h2>
</body>
</html>

 

 最终结构,库文件里面多出来一个 javassist-3.7.ga.jar,不知道其他人如何,每次我不加这个包,程序不能正常启动,这个包在struts2的 blank war包里面包含了

相关阅读 更多 +
排行榜 更多 +
翌日波奇狗的历险记手机版下载

翌日波奇狗的历险记手机版下载

休闲益智 下载
怪兽远征安卓版下载

怪兽远征安卓版下载

角色扮演 下载
谷歌卫星地图免费版下载

谷歌卫星地图免费版下载

生活实用 下载