struts2环境搭建
搭建一个最简单的struts2的环境:
一、MyEclipse右键new一个Web Project。
二、将struts2的jar复制黏贴到lib文件夹中。
三、Web.xml文件,从demo中复制黏贴<filter>和<filter-mapping>内容,其中<url-pattern>,默认写 “/*”,约定俗成、
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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>
四、从demo中复制struts.xml文件到src文件夹中,
<struts>标签中所有内容都注释掉,留下以备参考,
复制<package>的内容,去掉 <default-action-ref name="index" />
将<action> 标签内容修改如下,
<action name="hello">
<result>
/hello.jsp
</result>
</action>
将index.jsp改名hello.jsp
修改hello.jsp的body内容
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ 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>
五、把项目部署到tomcat中
六、访问http://localhost:8080/Struts2_0100/hello.action,其中Struts2_0100为项目名称,hello.action一定要写,可以简写为hello