Maven 构建 Struts2 项目

  1. 新建Java Web项目,标准的Maven目录
    Maven 构建 Struts2 项目
  2. classpath下添加log4j.xml或log4j.properties
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
    
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
        
        <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
           <layout class="org.apache.log4j.PatternLayout"> 
              <param name="ConversionPattern" value="%d %-5p %c.%M:%L - %m%n"/> 
           </layout> 
        </appender>
     
        <!-- specify the logging level for loggers from other libraries -->
        <logger name="com.opensymphony">
        	<level value="DEBUG" />
        </logger>
    
        <logger name="org.apache.struts2">
        	 <level value="DEBUG" />
        </logger>
      
       <!-- for all other loggers log only debug and above log messages -->
         <root>
            <priority value="INFO"/> 
            <appender-ref ref="STDOUT" /> 
         </root> 
        
    </log4j:configuration> 
    
  3. classpath下添加struts.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
    
    	<constant name="struts.devMode" value="true" />
    	
    	<package name="struts" extends="struts-default">
    		
    		<action name="index">
    			<result>/index.jsp</result>
    		</action>
    		
    	</package>
    
    </struts>
  4. web.xml中添加struts2过滤器
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Basic Struts2</display-name>
      <welcome-file-list>
        <welcome-file>index.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>
  5. 新建页面index.jsp 
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Struts 2 Application - Welcome</title>
    </head>
    <body>
    	<h3>Welcome To Struts 2!</h3>
    
    </body>
    </html>
  6. 配置pom.xml
    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>struts Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
      	<dependency>
    	    <groupId>org.apache.struts</groupId>
    	    <artifactId>struts2-core</artifactId>
    	    <version>2.3.16</version>
    	</dependency>
    	<dependency>
    	    <groupId>log4j</groupId>
    	    <artifactId>log4j</artifactId>
    	    <version>1.2.14</version>
    	</dependency>
      </dependencies>
      <build>
        <finalName>struts</finalName>
      </build>
    </project>
     finalName是项目名称
  7. 运行maven命令:
    mvn clean package
     生成struts.war,拷贝到Web容器中运行,访问http://localhost:8080/struts/index.action

你可能感兴趣的:(maven,struts2)