IntelliJ IDEA中使用綜合使用Maven和Struts2

在Intellij IDEA中手動使用Maven建立Web項目並引入Struts2

建立一個新的Maven項目





建好項目以後點擊左下角的enable auto import

html

項目部署


在Moudules中添加組件

配置webapp的路徑

配置web.xml的路徑

Facts: 表示當前項目的適配服務組件。可看到此項目已經是一個Web項目了。

Aftifacts: 這個Aftifacts描述了當前項目發佈的信息。如今進行添加,從Modeles中選擇。


預覽web exploded

說明:A: 如今Artifacts已有了發佈的項目了(idea中準確的說應是Modele) B:output root目錄描述了當前項目的編譯目錄及適配服務。
若有須要添加lib目錄

項目結構一覽
java

部署服務器



添加artifact



添加tomcat依賴包


web

添加Struts2.2.1依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.action</groupId>
    <artifactId>action</artifactId>
    <version>1.0-SNAPSHOT</version>

        <!-- 屬性配置 -->
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>

        <dependencies>

            <!-- struts2依賴包 -->
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-core</artifactId>
                <version>2.2.1</version>
            </dependency>

            <!-- junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.9</version>
                <scope>test</scope>
            </dependency>


        </dependencies>

        <build>
            <finalName>action</finalName>
        </build>

</project>

在src/main/java下添加一個包,包名爲controller

在controller包中添加以下的類login.javaapache

package controller;

public class login {

    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String execute() {
        if (username.equals("tuhooo") && password.equals("123")) {
            return "toTrue";
        }
        else {
            return "toFalse";
        }
    }

}

在webapp下添加以下的幾個jsp頁面:
login.jsptomcat

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form name="login" method="post" action="login.action">
        <p>
            用戶:
            <input type="text" name="username" id="textfield" />
            <br/>
            <br/>
            密碼:
            <input type="text" name="password" id="textfield2" />
            <br/>
            <br/>
            <input type="submit" name="button" value="提交" />
        </p>
    </form>
</body>
</html>

true.jsp服務器

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    welcome! :)
</body>
</html>

false.jspintellij-idea

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    wrong! :(
</body>
</html>

添加strutt配置文件

將其直接放在resources目錄下面
app

struts.xml的內容爲:webapp

<?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="default" extends="struts-default" namespace="/">

        <action name="login" class="controller.Login">
            <result name="toTrue">/true.jsp</result>
            <result name="toFalse">/false.jsp</result>
        </action>

    </package>

</struts>

配置web.xmljsp

<?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">
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

    <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>

</web-app>

java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
後來在Moudule中發現Maven的中添加的依賴包都沒有加進去

以前這個還能夠運行,而後就報這個錯了
com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration

後來分析是jass這個包有問題

相關文章
相關標籤/搜索