Maven+struts2+eclipse环境搭建

Maven+struts2+eclipse环境搭建

1、在cmd命令中输入如下命令
mvn archetype:generate -DgroupId=com.xueyou.testproject -DartifactId=testproject -Dpackage=com.xueyou.struts2 -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

2、修改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>com.xueyou.testproject</groupId>
  <artifactId>testproject</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>testproject 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>
    <!-- struts2核心依赖 -->  
    <dependency>  
        <groupId>org.apache.struts</groupId>  
        <artifactId>struts2-core</artifactId>  
        <version>2.3.4</version>  
        <type>jar</type>  
        <scope>compile</scope>  
    </dependency>  
    <!-- struts2零配置依赖 -->  
    <dependency>  
        <groupId>org.apache.struts</groupId>  
        <artifactId>struts2-convention-plugin</artifactId>  
        <version>2.3.4</version>  
        <type>jar</type>  
        <scope>compile</scope>  
    </dependency>  
    <!-- struts2的json依赖 -->  
    <dependency>  
        <groupId>org.apache.struts</groupId>  
        <artifactId>struts2-json-plugin</artifactId>  
        <version>2.3.4</version>  
        <type>jar</type>  
        <scope>compile</scope>  
    </dependency>
    <!-- 查看部署的aciton信息 -->
    <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-config-browser-plugin</artifactId>
    <version>2.3.20</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>testproject</finalName>
  </build>
</project>

3、通过cmd的cd命令进入文件夹:cd testproject

4、执行命令:mvn package。
这样环境的框架就打起来了,现在用eclipse导入工程

5、导入mavean工程之后会发现报错,原因是没有server runtime 对应的包,需要在library中加入包(一般是tomcat的包)

6、替换src/main/webapp/index.jsp文件
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>index.jsp</title>
</head>
<body>
<h1>hello world!!!</h1>
</body>
</html>

7、部署、运行:


8、配置struts2,修改src/main/webapp/WEB-INF/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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

9、在WEB-INF文件夹下创建文件夹content,在content文件夹中创建first.jsp文件
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>first.jsp</title>
</head>
<body>
<h1>First Jsp</h1>
</body>
</html>

10、在WEB-INF文件夹下创建classes文件夹,在classes文件夹中创建struts.xml文件(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>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="xueyoupackage" namespace="/" extends="struts-default">
		<action name="*">
			<result>/WEB-INF/content/{1}.jsp</result>
		</action>       
    </package>
</struts>

11、目录结构


12、重新部署、运行






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值