1、命令行执行以下语句
mvn archetype:create -DgroupId=com.watson -DartifactId=s2sh -DarchetypeArtifactId=maven- archetype-webapp
就会在当前目录下创建一个myWebapp的web工程
2、我们使用struts2框架,需要添加struts2的依赖到pom.xml里面。
3、我们使用jetty容器来运行我们的web项目,添加jetty的插件到pom.xml里面。
pom.xml
- <span style="font-family:Microsoft YaHei;"><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>com.watson</groupId>
- <artifactId>s2sh</artifactId>
- <packaging>war</packaging>
- <version>1.0-SNAPSHOT</version>
- <name>s2sh 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>
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-core</artifactId>
- <version>2.2.1</version>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.14</version>
- </dependency>
- <dependency>
- <groupId>javassist</groupId>
- <artifactId>javassist</artifactId>
- <version>3.8.0.GA</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>s2sh</finalName>
- <defaultGoal>install</defaultGoal>
- <plugins>
- <plugin>
- <groupId>org.mortbay.jetty</groupId>
- <version>6.1.22</version>
- <artifactId>maven-jetty-plugin</artifactId>
- <configuration>
- <contextPath>/</contextPath>
- <scanIntervalSeconds>3</scanIntervalSeconds>
- <connectors>
- <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
- <port>7002</port>
- <maxIdleTime>60000</maxIdleTime>
- </connector>
- </connectors>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
- </span>
4、然后进入工程目录下执行mvn compile,编译成功则表示配置正确。
5、在s2sh\src\main下面添加Java文件夹存放java文件,添加resources文件夹存放配置文件,webapp是 web相关的文件。
在webapp文件夹下打开web.xml添加struts2的过滤器。
- <span style="font-family:Microsoft YaHei;"><!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>s2 Web Application</display-name>
- <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>
- </span>
6、在java目录下新建一个类HelloWorldAction
- <span style="font-family:Microsoft YaHei;">package com.watson.s2sh.action;
-
- import com.opensymphony.xwork2.ActionSupport;
-
- public class HelloWorldAction extends ActionSupport {
- private static final long serialVersionUID = 1L;
-
- public String execute() throws Exception {
- System.out.println("hero world");
- return SUCCESS;
- }
- }</span>
7、在webapp的目录下添加一个jsp文件,HelloWorld.jsp,内容如下
- <span style="font-family:Microsoft YaHei;"><%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loo
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Hello World!</title>
- </head>
- <body>
- <h2>
- <s:property value="messageStore.message" />
- </h2>
- </body>
- </html>
- </span>
8),在resources目录下添加struts.xml文件,内容如下:
- <span style="font-family:Microsoft YaHei;"><?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>/index.jsp</result>
- </action>
- <action name="hello" class="com.watson.s2sh.action.HelloWorldAction" method="execute">
- <result name="success">/HelloWorld.jsp</result>
- </action>
- </package>
- </struts>
- </span>
9、在工程目录下面/s2sh,使用mvn jetty:run来运行web应用。
10、启动完成后,访问如下链接:
http://localhost:7002/s2sh/hello:访问HelloWorldAction,跳转到HelloWorld.jsp.
http://localhost:7002/s2sh/index:访问index.jsp页面。