Loading...
Struts2专题之一:创建一个简单Struts2 WEB应用

Struts2专题 2015/12/30 Struts2

1、创建一个java maven web项目

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>cn.imethan.web.struts2</groupId>
	<artifactId>imethan-struts2</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>imethan-struts2 Maven Webapp</name>
	<url>http://maven.apache.org</url>	

	<dependencies>		
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<finalName>imethan-struts2</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>


2、往pom.xml中添加Struts2和log4j2的 maven依赖

<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.3.24.1</version>
		</dependency>
		
		<!-- log4j2 begin -->
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>2.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j-impl</artifactId>
			<version>2.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-jcl</artifactId>
			<version>2.4</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-web</artifactId>
			<version>2.4</version>
		</dependency>
		<!-- log4j2 end -->


3、将log4j2.xml配置文件放到“WEB-INF”目录下

<?xml version="1.0" encoding="UTF-8"?>
<!-- status表示log4j事件输出控制台级别,分别有"trace", "debug", "info", "warn", "error","fatal" -->
<!-- monitorInterval表示log4j会自动监听配置文件的变动,最小监听时间为5秒 -->
<Configuration status="WARN" monitorInterval="5">

   <!-- 通用属性配置 -->
  <Properties>
    <Property name="appname">imethan-web</Property>
  </Properties>
  
  <!-- 默认过滤等级 -->
  <ThresholdFilter level="trace"/>
  
  <Appenders>
  	<!-- 控制台输出 -->
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
    
    <!-- 每日文件记录 -->
	<RollingFile name="RollingFile" fileName="logs/${appname}.log" filePattern="logs/${appname}-%d{MM-dd-yyyy}.log.gz">
      <BurstFilter level="warn" rate="16" maxBurst="100"/>
      <PatternLayout>
        <pattern>%d %p %c{1.} [%t] %m%n</pattern>
      </PatternLayout>
      <TimeBasedTriggeringPolicy />
    </RollingFile>
	<RollingFile name="RollingFileError" fileName="logs/${appname}-error.log" filePattern="logs/${appname}-error-%d{MM-dd-yyyy}.log.gz">
      <ThresholdFilter level="error" />
      <PatternLayout>
        <pattern>%d %p %c{1.} [%t] %m%n</pattern>
      </PatternLayout>
      <TimeBasedTriggeringPolicy />
    </RollingFile>
    
    <!-- NoSql数据库记录 
    <NoSql name="databaseAppender" ignoreExceptions="false">
      <MongoDb databaseName="database" collectionName="applicationLog" server="127.0.0.1"
               username="" password="" />
    </NoSql>
    -->
        
  </Appenders>
  
  <Loggers>
  	<!-- 全局日志记录 -->
    <Root level="debug">
      <AppenderRef ref="Console"/>
      <AppenderRef ref="RollingFile"/>
      <AppenderRef ref="RollingFileError"/>
    </Root>
    
  </Loggers>
</Configuration>


4、往web.xml配置文件中添加Struts2 Servlet过滤器、log4j2监听器和过滤器

    <!-- log4j2 web2.0 begin -->
    <listener>
        <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
    </listener>
    <filter>
        <filter-name>log4jServletFilter</filter-name>
        <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>log4jServletFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>
    <!-- log4j2 web2.0 end-->

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

5、将struts2.xml放到“src/main/resources”下,并且在“WEB-INF/view”目录下创建index.jsp文件

<?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="basicstruts2" extends="struts-default"> 
        <action name="index">
            <result>/WEB-INF/view/index.jsp</result>
        </action> 
    </package> 
</struts>


<html>
	<body>
	<h2>Welcome to Struts 2!</h2>
	</body>
</html>


6、 Jetty运行启动,浏览器访问:http://localhost:8080/imethan-struts2/index.action,返回index.jsp页面内容:

Welcome to Struts 2!


参考文档

http://struts.apache.org/docs/getting-started.html





Comments