Struts2基础教程一:如何创建web项目

来源:互联网 发布:方正败诉淘宝网店 编辑:程序博客网 时间:2024/05/17 00:02

Struts2基础教程一:如何创建web项目

目录:

  1. 演示环境
  2. maven构建web工程
  3. 原始web工程演示结果
  4. web整合Struts2的步骤
  5. Struts2 web演示结果

[一]、演示环境

  • java version “1.6.0_18″
  • Maven v 3.0.4
  • Eclipse 3.7
  • Struts 2.3.4.1

[二]、maven构建web工程

比如我的工作目录路径:D:\workspace_sun\struts2-tutorial\

按 win+R 键,输入 cmd 回车进入控制台界面,cd 切换到工作目录下运行如下命令:

1mvn archetype:create -DgroupId=com.micmiu.struts2.demo -DartifactId=struts2-basic-demo -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

创建成功后,cd 切换到目录 struts2-basic-demo\ 下,把maven项目转化为eclipse项目:

1mvn eclipse:eclipse -Dwtpversion=1.0

然后在eclipse 中选择 File -> Import… -> Exiting Maven Projects 导入项目。

配置修改相关的 Java Build Path ,最后的目录结构如下:

D:\WORKSPACE_SUN\STRUTS2-TUTORIAL\STRUTS2-BASIC-DEMO\SRC├─main│  ├─java│  ├─resources│  └─webapp│      │  index.jsp│      ││      └─WEB-INF│              web.xml│└─test    ├─java    └─resources

Eclipse 中的目录结构图:

[三]、原始web工程演示结果

修改 web.xml 文件如下:

1<?xml version="1.0" encoding="UTF-8"?>
2<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4    xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
5 
6    <display-name>struts2 demo www.micmiu.com</display-name>
7 
8</web-app>

修改 index.jsp 文件如下:

1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4<title>Welcome to micmiu.com</title>
5</head>
6<body>
7    <h1>Hello World!</h1>
8    <h3>
9        see more:<a href="http://www.micmiu.com">www.micmiu.com</a>
10    </h3>
11</body>
12</html>

Run on Server 启动项目后,浏览器中输入:http://localhost:8082/struts2-basic-demo/ 回车:

看到上述截图内容表示web项目已经配置启动成功。

[四]、web整合Struts2的步骤

在配置文件pom.xml 的 <dependencies> 节点中增加 strtus2 和 log4j 相关的依赖关系,内容如下:

1<dependency>
2    <groupId>org.apache.struts</groupId>
3    <artifactId>struts2-core</artifactId>
4    <version>2.3.4.1</version>
5    <type>jar</type>
6    <scope>compile</scope>
7</dependency>
8<dependency>
9    <groupId>log4j</groupId>
10    <artifactId>log4j</artifactId>
11    <version>1.2.14</version>
12    <type>jar</type>
13    <scope>compile</scope>
14</dependency>

web.xml 中添加Struts 2 Servlet Filter,修改如下:

1<?xml version="1.0" encoding="UTF-8"?>
2<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4    xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
5 
6    <display-name>struts2 demo www.micmiu.com</display-name>
7 
8    <filter>
9        <filter-name>struts2</filter-name>
10        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
11    </filter>
12 
13    <filter-mapping>
14        <filter-name>struts2</filter-name>
15        <url-pattern>/*</url-pattern>
16    </filter-mapping>
17 
18    <welcome-file-list>
19        <welcome-file>index.jsp</welcome-file>
20    </welcome-file-list>
21 
22</web-app>

ps:Struts 2.1.3之前就版本的Filter 配置如下:

1...
2<filter>
3    <filter-name>struts2</filter-name>
4    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
5...

在 src/main/resource/ 目录下创建struts2的配置文件:struts.xml

1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE struts PUBLIC
3    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4    "http://struts.apache.org/dtds/struts-2.0.dtd">
5<struts>
6    <constant name="struts.devMode" value="true" />
7    <package name="basicstruts2" extends="struts-default">
8        <action name="index">
9            <result>/index-struts2.jsp</result>
10        </action>
11    </package>
12</struts>

在 src/webapp/ 目录下创建JSP文件:index-struts2.jsp

1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4<title>Welcome to micmiu.com</title>
5</head>
6<body>
7    <h1>Hello Struts2!</h1>
8    <h3>
9        see more:<a href="http://www.micmiu.com">www.micmiu.com</a>
10    </h3>
11</body>
12</html>

[五]、Struts2 web演示结果

Run on Server 启动项目后,浏览器中输入:http://localhost:8082/struts2-basic-demo/  和http://localhost:8082/struts2-basic-demo/index 回车:

看到上述截图内容表示 Struts2 web项目已经配置启动成功。

原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]

本文链接地址: http://www.micmiu.com/j2ee/struts/struts2-basic-web/


0 0
原创粉丝点击