Struts2.5在Eclipse的配置问题

来源:互联网 发布:php集成环境包 编辑:程序博客网 时间:2024/06/14 16:24

Struts2.5在Eclipse的配置问题


1、首先建立Web动态工程,在最后一步勾选web.xml选项(没选也没关系,在WEB-INF中创建web.xml文件)

2、向WEB-INF/lib中添加(至少)一下jar文件(可以在官网上下载Struts2)

3、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  <display-name>Struts2Demo4</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.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>Struts2</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping></web-app>

注意:Struts2.5版本以后,内容改变

4、配置struts.xml文件(在Java Resources/src目录下创建struts.xml文件)内容:

<?xml version="1.0" encoding="UTF-8"?>    <!DOCTYPE struts PUBLIC                "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"        "http://struts.apache.org/dtds/struts-2.5.dtd">  <struts>    <include file="structs-default.xml"></include>      <!-- 指定默认编码集 -->    <constant name="struts.i18n.encoding" value="UTF-8"/>      <!-- 指定Structs2处理的请求后缀 -->    <constant name="structs.action.extension" value="do,action"/>    <!-- 设置浏览器是否缓存静态内容,开发阶段应关闭,生产阶段打开,默认为打开 -->    <constant name="structs.serve.static.browserCache" value="false"/>    <!-- 当struts配置文件修改后,系统是否重新加载该文件,默认为false,开发阶段应打开 -->    <constant name="struts.configuration.xml.reload" value="true"/>    <!-- 开发阶段可以打印出更详细的错误信息 -->    <constant name="struts.devMode" value="true"/>    <!-- 是否开启方法调用 -->    <constant name="struts.enable.DynamicMethodInvocation" value="false" />      <!--添加包 -->    <package name="gzy" namespace="/" extends="struts-default">//gzy为包的名字    //以下内容为例子        <action name="hello" class="gzy.Hello" method="test">//method为使用Hello中的method方法            <!-- 定义三个逻辑视图和物理资源之间的映射 -->            <result name="error">/error.jsp</result>            <result name="index">/index.jsp</result>        </action>    </package>  </struts>    

5、创建一个简单的登录页面,login.jsp,index.jsp,error.jsp
login.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>login</title></head><body><form action="hello" method="post">用户名:<input type="text" name="username"/><br>密码 :   <input type="text" name="password"/><br><input type="submit" value="提交"/></form></body></html>

action=”hello”对应struts.xml action
index:<body>登录成功</body>
error:<body>登录失败</body>

5、创建包(此处创建的是gzy包),再创建Hello.java文件

package gzy;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class Hello extends ActionSupport{    HttpServletRequest req=ServletActionContext.getRequest();    String username=req.getParameter("username");    String password=req.getParameter("password");    public String test(){        if(username.equals("A")&&password.equals("B")){            return "index";    //返回值对应的是struts.xml的result name                    }        else            return "error";    }}

此时整个工程文件的目录为:


23333….配置问题解决了很长时间,网上也参考了很多资料,在配置过程中也遇到很多问题,但如果按照以上步骤完成的话,是没有错误的。
在配置过程中我遇到如下问题:
1、导入的包不完整,这个问题是遇到的最多得,不清楚应该导入哪些,官网上有提供最小的安装包(大概4M)中就是必要的jar文件。此处xwork jar文件已经融入到struts.core文件中,不必纠结找不到
2、web.xml配置不对,因为借助的参考书是2.3版本的,但strut2.5版本进行了改动
2.5以前:
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>

2.5以后:
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>

3、struts.xml配置不对,上述给出的配置并不是全都必要,必要的是:
<constant name="struts.enable.DynamicMethodInvocation" value="false" />

刚开始学Struts框架,有些地方也不是很清楚。。。/(ㄒoㄒ)/~~不过希望能帮助到大家