• 在idea中用maven搭建struts2项目(基础)


    1  新建maven项目,ctrl+shift+alt+s调出 project Structure设置菜单,点击modules->添加web项目

      设置path路径,将web改为srcmainwebapp目录

      path relative to deployment root的设置同上

    2  倒入struts2的核心依赖

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


    3  引入其他插件,并<packaging>war包,才可以在tomcat运行
      
    <build>
    <plugins>
    <!-- java编译插件 -->
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>

    <!-- 配置Tomcat插件 -->
    <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
    <port>8080</port>
    <path>/</path>
    <!--这是get请求的乱码处理-->
    <uriEncoding>utf-8</uriEncoding>
    </configuration>
    </plugin>

    </plugins>

    </build>
    还有dom4j的依赖
    <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.4.1</version>
    </dependency>


    4   web.xml的配置如下
      
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app 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_3_0.xsd"
    version="3.0">
    <display-name>Archetype Created Web Application</display-name>
    <!--引入核心过滤器-->
    <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <init-param>
    <param-name>config</param-name>
    <param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
    </init-param>
    </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>

    5  struts.xml的配置如下,在resource目录下
    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

    <struts>
    <package name="default" extends="struts-default" namespace="/">
    <action name="login" class="cn.itcast.action.DemoAction" method="success">
    <result name="success">success.jsp</result>
    </action>
    </package>
    </struts>

    6   建立action实现类
    package cn.itcast.action;
    public class DemoAction {
    public String success(){

    System.out.println("成功");
    return "success";
    }

    }

    7  在webappa下创建index和success.jsp即可

    8  启动tomcat,访问locahost:8080/login即可



    第一次搭建,出现很多bug,大致总结如下:

    1  Unable to process Jar entry [module-info.class] from Jar [jar:file:/D:/Program%20Files/maven/apache-maven-3.5.2/repository/org/apache/logging/log4j/log4j-api/2.10.0/log4j-api-2.10.0.jar!/] for annotations
      解决办法:按报错的路径找到包,删除,重新导入maven依赖

    2  提示缺少dom4j2-core包,将dom4j的依赖导入

    3  classNotFound异常
        看一下struts2的版本,在2.5之前是org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter,之后需要把ng去掉
    4  No mapping found for dependency [type=com.opensymphony.xwork2.ObjectFactory, name='default'] in publc
      这个异常是web.xml配置出错
    <init-param>
    <param-name>config</param-name>
    <param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
    </init-param>
    上面的struts-default.xml,struts-plugin.xml,struts.xml都要加上,不能少



  • 相关阅读:
    Python 15 爬虫(一)
    Python 14 Mysql数据库(二)
    Python 13 JQuery&Bootstrp
    Python 12 CSS&JavaScript&DOOM
    Python 11 HTML
    Python 10 MySQL数据库(一)
    Python 9 Redis
    Python 8 协程
    Python 7 并发编程
    SNMP协议详解
  • 原文地址:https://www.cnblogs.com/zxq-Study-Java/p/9960609.html
Copyright © 2020-2023  润新知