码迷,mamicode.com
首页 > 其他好文 > 详细

创建一个struts2的HelloWorld

时间:2016-11-04 01:33:44      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:依赖   home   包含   depend   nload   default   解压   blank   not   

1、下载struts2的jar包

http://struts.apache.org/download.cgi#struts255

下载一个稳定版本Struts 2.3.31

里面提供了maven jar

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.31</version>
</dependency>

 

2、创建一个动态web工程导入这些jar包

修改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 Demo</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.jsp</welcome-file>
    </welcome-file-list>

    

</web-app>

可以参照struts2-blank中的配置

 

3、在src目录下创建struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <package name="default" namespace="/" extends="struts-default">

        <default-action-ref name="index" />

        <global-results>
            <result name="error">/WEB-INF/jsp/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception"
                result="error" />
        </global-exception-mappings>

        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>

        <action name="products-input">
            <result>/WEB-INF/pages/products.jsp</result>
        </action>
        
        <action name="products-detail" class="com.hy.Products">
            <result name="success">/WEB-INF/pages/detail.jsp</result>
        </action>

    </package>

    <!-- Add packages here -->

</struts>

参照truts2-blank中的配置 

struts.enable.DynamicMethodInvocation指的是是否支持模糊匹配方法名

这里面有一个pojo

package com.hy;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class Products{
    
    private String productID;
    
    private String productName;
    
    private String productDesc;

    public String getProductID() {
        return productID;
    }

    public void setProductID(String productID) {
        this.productID = productID;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductDesc() {
        return productDesc;
    }

    public void setProductDesc(String productDesc) {
        this.productDesc = productDesc;
    }
    
    public String detail() {
        return SUCCESS;
    }
    
    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
    
}

两个跳转的页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>商品录入</title>
</head>
<body>
    <form action="products-detail" method="post">
        productID:<input type="text" name="productID" /><br>
        productName:<input type="text" name="productName" /><br>
        productDesc:<input type="text" name="productDesc" /><br> 
        <input type="submit" value="submit"><br>
    </form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>商品详细</title>
</head>
<body>
productID:<input type="text" name="productID" value="${productID}"/><br>
productName:<input type="text" name="productName" value="${productName}"/><br>
productDesc:<input type="text" name="productDesc" value="${productName}"/><br> 
</body>
</html>

大概就这样

 

注意:

导入struts2-blank到eclipse时报错,这是一个maven工程

首先要安装maven插件(高版本eclipse自带)

http://maven.apache.org/download.cgi

选择一个较新的版本下载

下载完成后解压到本地,配置m2_home

D:\Program Files\apache-maven-3.3.9

再配置bin目录到path变量中

测试 cmd mvn -version

现在import exist maven projects 导入后项目编译报错

技术分享

选择直接忽略。具体原因未知

 

这时候maven update project 把改下的jar包下载到maven jar包仓库中

启动tomcat会发现struts2核心包classnotfound

需要把maven dependence加入到部署环境中

 

运行之后可能会出现jsp报错,这是因为maven中包含了较低版本的tomcat jar删除依赖后启动成功

 

创建一个struts2的HelloWorld

标签:依赖   home   包含   depend   nload   default   解压   blank   not   

原文地址:http://www.cnblogs.com/hy87/p/6028611.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!