eclipse下配置struts2,超详细!(包括war文件不能解压问题)
此处也参考了http://www.cnblogs.com/hpuCode/archive/2012/07/12/2588305.html的内容,个人觉得确实写得不错
一、下载struts2
首先到官网下载struts2,我下载的是struts-2.3.34版本,官网地址:http://struts.apache.org/download.cgi#struts2513,只需要下载struts-2.3.34-all.zip就可以了,它包含了其他所有的。
二、解压struts-2.3.34-all.zip
效果如下图
三、解压struts2-blank.war文件(如果没有遇到war不能解压的,可以跳过)
注:由于我本人的电脑里面并不支持解压此.war文件,故下面我特此说明我采用的方法,很简单,但实用
1.解压了struts-2.3.34-all.zip后,首先,点击apps,里面有5个文件,如下图(图标因为解压过的原因,有所改变,请忽略)
2.选择struts2-blank.war右键选择解压,这时会发现并没有解压这个选项只有压缩选项,这时怎么办呢,下面我重点说一下我的处理方法
2.1 右键选择->添加到"struts2-blank.rar"(T),压缩后如图
2.2 双击struts2-blank.rar后,如图
2.3此时再双击此文件,有如图
2.4 对这三个文件挨个解压到指定文件夹,我选择的是D:\ssh\structs2\2.3version\struts-2.3.34-all\struts-2.3.34\apps,即当前文件夹,解压后如图
四、下面进入重点,即在eclipse下配置
4.1 将相应的jar文件导入到lib文件下
首先在eclipse中新建Dynamic Web Project,如下,然后点击finish
然后,在计算机中找到刚才解压了的struts2-blank文件夹
选择WEB-INF
选择lib
全选复制粘贴到eclipse项目中的WebContent->WEB-INF->lib文件夹下
1
2
3
4.2 修改web.xml 文件
找到struts2-blank文件夹下WEB-INF中的web.xml,复制粘贴到eclipse中lib的文件夹下
点击打开web.xml文件,会有如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank</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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- Restricts access to pure JSP files - access available only via Struts action -->
<security-constraint>
<display-name>No direct JSP access</display-name>
<web-resource-collection>
<web-resource-name>No-JSP</web-resource-name>
<url-pattern>*.jsp</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>no-users</role-name>
</auth-constraint>
</security-constraint>
<security-role>
<description>Don't assign users to this role</description>
<role-name>no-users</role-name>
</security-role>
</web-app>
我修改后如下
<<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank002</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>
注:其中的重点就是filter以及filter-mapping,原文件中的其它内容可以先注释掉,以后可能会用到,此处只是为了看起来更简洁
4.3 修改struts.xml文件
找到struts2-blank中的WEB-INF->src->java中的struts.xml文件,并复制粘贴到eclipse中的src文件夹下
点击打开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="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
<include file="example.xml"/>
<!-- Add packages here -->
</struts>
我修改后的文件,如下
<?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>
<package name="default" namespace="/" extends="struts-default">
<action name="hello" >
<result >/test/hello.jsp</result>
</action>
</package>
</struts>
注:建议其它多余部分注释掉,而不是删除。
到此,重点戏就结束了
五、新建.jsp文件,测试是否成功
在WebContent下新建test文件夹,并在其中新建hello.jsp文件,我的代码如下
<%@ page language="java" pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>hello struts</title>
</head>
<body>
struts2 success!
</body>
</html>
启动Tomcat,在浏览器中输入http://localhost:8081/Struts Blank002/hello,或者http://localhost:8081/Struts Blank002/hello.action,
可以得到
到此,全部结束
第一次,写博客,不足之处多多见谅,喜欢就点个赞吧!