工具配置-idea中利用maven创建Struts2项目记录

idea+maven+Struts2的踩坑记录

常见jar说明:

struts2-core-2.3.1.1.jar:Struts 2框架的核心类库
 
xwork-core-2.3.1.1.jar:Command模式框架,WebWork和Struts2都基于xwork
 
ognl-3.0.3.jar:对象图导航语言(Object Graph Navigation Language),
 
struts2框架通过其读写对象的属性
 
freemarker-2.3.18.jar:Struts 2的UI标签的模板使用FreeMarker编写
 
commons-logging-1.1.x.jar:ASF出品的日志包,Struts 2框架使用这个日志 包来支持Log4J和JDK 1.4+的日志记录。
 
commons-fileupload-1.2.2.jar: 文件上传组件,2.1.6版本后需要加入此文件
 
commons-io-2.0.1.jar:传文件依赖的jar包
 
commons-lang-2.5.jar:对java.lang包的增强

引入Struts 2的操作流程

创建maven

image-20220504095209135

创建项目后的更改

如果是直接引用的以前的项目配置,需要在该部分做更改

  1. 确定pom.xml中的版本

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    
  2. junit单元测试模块

    <dependencies>
        <dependency>
        <groupId>junit</groupId>
    	<artifactId> junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    
  3. web.xml中的配置

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    </web-app>
    

添加Struts 2项目依赖

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

修改<version>2.3.14.1</version>中的版本号即可,maven可以根据版本号下载对应的依赖。

配置Struts 2环境

配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <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>

注意<web-app>标签内的web版本,建议<web-app>标签不改,直接将后面的部分加进去即可。

filter-class 配置报错

idea创建struct2项目web.xml文件时有可能报错(Cannot resolve class ‘StrutsPrepareAndExecuteFilter‘)

当前使用的是低版本Struts 2(2.3.14.1),因此需要做以下更改:

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>

修改为:

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>

低版本需要添加.ng,高版本不需要。

配置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>
    <package name="basicstruts" extends="struts-default">
        <action name="index">
            <result>index.jsp</result>
        </action>
    </package>
</struts>

关于idea的terminal中文乱码问题

解决方案:

  1. 打开File –> settings -> File encoding,都设置为UTF-8。

    image-20220504112524948

  2. Help -> Edit Custom VM Options,在打开的idea64.exe.vmoptions文件的末尾加上一行-Dfile.encoding=UTF-8然后保存重启IDEA。

    image-20220504112731906

posted @ 2022-05-04 12:03  sherlson  阅读(138)  评论(0编辑  收藏  举报