1. 程式人生 > >用idea搭建struts2.5

用idea搭建struts2.5

一.STRUTS介紹

二.搭建環境(使用INTELLIJ IDEA+MAVEN+STRUTS2)

1.建立MAVEN+STRUTS2專案
1).建立Maven專案:maven快速入門

2).設定Groupld和Artfactld名稱
2.新增配置檔案,自動下載必要JAR包
commons-fileupload-1.2.2.jar 【檔案上傳相關包】
commons-io-2.0.1.jar
struts2-core-2.3.4.1.jar 【struts2核心功能包】
ognl-3.0.5.jar 【Ognl表示式功能支援表】
commons-lang3-3.1.jar 【struts對java.lang包的擴充套件】
freemarker-2.3.19.jar 【struts的標籤模板庫jar檔案】
javassist-3.11.0.GA.jar 【struts對位元組碼的處理相關jar】
配置檔案引入

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

這裡寫圖片描述

3.WEB.XML中引入STRUTS核心功能——配置過濾器

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app> <display-name>Archetype Created Web Application</display-name> <!--引入核心過濾器--> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.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>

這裡寫圖片描述
4.開發ACTION
LoginAction.java

package com.huan.struts.action;

/**
 * Created by 馬歡歡 on 2017/6/19.
 */
public class LoginAction {
    public String success(){
        System.out.println("成功訪問action,請求正在處理中");
        System.out.println("呼叫service");
        return "success";
    }
}

success.jsp

<%--
  Created by IntelliJ IDEA.
  User: 馬歡歡
  Date: 2017/6/19
  Time: 21:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>success成功跳轉到該頁面</title>
</head>
<body>
</body>
</html>

5.配置ACTION——SRC/STRUTS.XML

struts2.xml

<?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="null"  extends="struts-default">
        <action name="login" class="com.huan.struts.action.LoginAction" method="success">
            <result name="success">success.jsp</result>
        </action>
    </package>
</struts>

1.建立MAVEN+STRUTS2專案步驟:

1).建立Maven專案:maven快速入門

2).設定Groupld和Artfactld名稱
這裡寫圖片描述

2.新增配置檔案,自動下載必要JAR包

commons-fileupload-1.2.2.jar 【檔案上傳相關包】
commons-io-2.0.1.jar
struts2-core-2.3.4.1.jar 【struts2核心功能包】
ognl-3.0.5.jar 【Ognl表示式功能支援表】
commons-lang3-3.1.jar 【struts對java.lang包的擴充套件】
freemarker-2.3.19.jar 【struts的標籤模板庫jar檔案】
javassist-3.11.0.GA.jar 【struts對位元組碼的處理相關jar】
配置檔案引入
pom.xml

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

3.webxml中引入struts核心功能——配置過濾器

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--引入核心過濾器-->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.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>

4.開發ACTION
LoginAction.java



package com.huan.struts.action;

/**
 * Created by 馬歡歡 on 2017/6/19.
 */
public class LoginAction {
    public String success(){
        System.out.println("成功訪問action,請求正在處理中");
        System.out.println("呼叫service");
        return "success";
    }
}

success.jsp


<%--
  Created by IntelliJ IDEA.
  User: 馬歡歡
  Date: 2017/6/19
  Time: 21:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>success成功跳轉到該頁面</title>
</head>
<body>
</body>
</html>

5.配置ACTION——SRC/STRUTS.XML

struts2.xml

<?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="null"  extends="struts-default">
        <action name="login" class="com.huan.struts.action.LoginAction" method="success">
            <result name="success">success.jsp</result>
        </action>
    </package>
</struts>

這裡寫圖片描述

這裡寫圖片描述
啟動成功
訪問成功
這裡寫圖片描述

原網址