天天看点

Intellij IDEA 配置Struts2 运行HellowordIntellij IDEA 配置Struts2

Intellij IDEA 配置Struts2

目录

Intellij IDEA 配置Struts2

准备工作:

下载jar包

创建工程:​

写一个Helloword

创建hello.jsp

创建helloword.jsp

创建一个Hello.class

struts.xml配置

web.xml配置

准备工作:

下载官网的Struts2的jar包

确保以前的Tomcat是可以使用的

下载jar包

官网:

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

Intellij IDEA 配置Struts2 运行HellowordIntellij IDEA 配置Struts2

第一个是完整jar包,不过很大,初学不太适合,后面这个是min版,有最核心的8个包。

下载的是.zip,解压到自己能找到的路径:

Intellij IDEA 配置Struts2 运行HellowordIntellij IDEA 配置Struts2

准备阶段完成

创建工程:
Intellij IDEA 配置Struts2 运行HellowordIntellij IDEA 配置Struts2

第三步的意义是:选择Struts库,第一项是用自己下载的包,第二项是自动下载,到这里时我们已经下载好包了。

点击第四步,找到之前解压的jar包,并选中。点击OK往下走。

Intellij IDEA 配置Struts2 运行HellowordIntellij IDEA 配置Struts2

到这时我们已经创建好工程,接下来进行简单配置:

快捷键打开添加包的设置页。Shift+Ctrl+Alt+S,或者找file ——> Projetc Structure ——> Artifacts

Intellij IDEA 配置Struts2 运行HellowordIntellij IDEA 配置Struts2

双击第二步中的文件,(自动添加到Web/lib),双击后的效果:

Intellij IDEA 配置Struts2 运行HellowordIntellij IDEA 配置Struts2

写一个Helloword

创建hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<a href="Hello.action" target="_blank" rel="external nofollow" >Hello Word</a>
</body>
</html>
           

创建helloword.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello Word</title>
</head>
<body>
<h1>Hello Word</h1>
</body>
</html>
           

创建一个Hello.class

package com.struts;

import com.opensymphony.xwork2.Action;

public class Hello implements Action {
    @Override
    public String execute() throws Exception {
        return "OK";
    }
}
           

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="Hello_Dome" extends="struts-default" namespace="/">
        <action name="Hello" class="com.struts.action.Hello">
            <result name="OK">
                helloword.jsp
            </result>
        </action>
    </package>
</struts>
           

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>
</web-app>
           

因为2.5.X版本将包名改变了,所以将默认的:

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

改为

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

到这里就全部配置好了,来启动一下Tomcat

Intellij IDEA 配置Struts2 运行HellowordIntellij IDEA 配置Struts2

点击Helloword跳转以下界面

Intellij IDEA 配置Struts2 运行HellowordIntellij IDEA 配置Struts2

到这里就算是成功配置运行了一个Struts2项目