搭建一个最简单的struts2的环境:css
1、MyEclipse右键new一个Web Project。html
2、将struts2的jar复制黏贴到lib文件夹中。java
3、Web.xml文件,从demo中复制黏贴<filter>和<filter-mapping>内容,其中<url-pattern>,默认写 “/*”,约定俗成、web
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts2_0100</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.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>
4、从demo中复制struts.xml文件到src文件夹中,apache
<struts>标签中全部内容都注释掉,留下以备参考,tomcat
复制<package>的内容,去掉 <default-action-ref name="index" />
将<action> 标签内容修改以下,app
<action name="hello">
<result>
/hello.jsp
</result>
</action>jsp
将index.jsp更名hello.jspide
修改hello.jsp的body内容ui
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>hello!</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> hello !!! <br> </body> </html>
5、把项目部署到tomcat中
6、访问http://localhost:8080/Struts2_0100/hello.action,其中Struts2_0100为项目名称,hello.action必定要写,能够简写为hello