搭建一個最簡單的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