怎样在eclipse中添加struts框架

如题所述

步骤如下:

一、下载struts包

百度直接搜“struts2”,进入Apache官网下载,此处下载的是struts-2.3.20-all.zip。


二、新建web项目

打开Eclipse新建一个Dynamic Web Project,项目名为:struts2。


三、向Web项目中添加struts2框架

1、解压struts-2.3.20-all.zip,在lib文件加下找到以下jar文件:

commons-fileupload-1.3.1.jar

commons-io-2.2.jar

commons-lang3-3.2.jar

freemarker-2.3.19.jar

javassist-3.11.0.GA.jar

ognl-3.0.6.jar

struts2-core-2.3.20.jar

xwork-core-2.3.20.jar


2、把以上文件,复制粘贴到第二步新建的Web项目下的WebContent/WEB-INF/lib路径下。


3、修改WebContent/WEB-INF路径下的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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 <display-name>struts 2</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>
 
 <!-- 以下是添加struts2框架 -->
 <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>


四、建立struts2项目文件

         1、在项目中的JavaResources/src路径下新建RegisterCheck.java跟RegisterAction.java文件(属于com.struts2包):

RegisterCheck.java代码:

package com.struts2;
 
publicclassRegisterCheck
{
    publicboolean check(String psd,String repsd)
    {
        if (psd.equals(repsd))
        {
            returntrue;
        }
        else
        {
            returnfalse;
        }
    }
}


RegisterAction.java代码:

package com.struts2;
 
publicclassRegisterAction
{
    private String usernaem;
    private String psd;
    private String repsd;
    public String getUsernaem()
    {
        returnusernaem;
    }
    publicvoid setUsernaem(Stringusernaem)
    {
        this.usernaem = usernaem;
    }
    public String getPsd()
    {
        returnpsd;
    }
    publicvoid setPsd(String psd)
    {
        this.psd = psd;
    }
    public String getRepsd()
    {
        returnrepsd;
    }
    publicvoid setRepsd(String repsd)
    {
        this.repsd = repsd;
    }
    
    public String execute() throws Exception
    {
        RegisterCheckrc=newRegisterCheck();
        if (rc.check(getPsd(),getRepsd()))
        {
            return"success";
        }
        else
        {
            return"failure";
        }
    }


2、在项目中的JavaResources/src路径下新建一个struts.xml文件,并在此文件中对项目的Action进行配置:

struts.xml文件内容:

<?xml version="1.0"encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="struts2" extends="struts-default">
        <action name="register" class="com.struts2.RegisterAction">
            <result name="success">/success.jsp</result>
            <result name="failure">/failure.jsp</result>
        </action>
    </package>
</struts>


3、在项目下的WebContent目录下新建register.jsp、success.jsp和failure.jsp文件:

register.jsp代码:

<%@ page language="java"contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 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>Insert title here</title>
</head>
<body>
<center>
    <form action="register" method="post">
        用户名:<input type="text" name="username"><br>
        密码:<input type="password" name="psd"><br>
        确认密码:<input type="password" name="repsd"><br>
        <input type="submit" value="提交">
        <input type="reset">
    </form>
</center>
</body>
</html>


success.jsp代码:

<%@ page language="java"contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 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>Insert title here</title>
</head>
<body>
<center>
    <h1>注册成功</h1>
</center>
</body>
</html>


failure.jsp代码:

<%@ page language="java"contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 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>Insert title here</title>
</head>
<body>
<center>
    <h1>注册失败</h1>
</center>
</body>
</html>


五、运行Web项目

         部署好web项目后,在浏览器输入“http://localhost:8080/struts2/register.jsp”,即可运行项目,输入数据测试是否配置好。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-12-05
1.框架搭建
1.1 将struts2中的jar文件导入到项目中
commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar,freemarker-2.3.15.jar,ognl-2.7.3.jar
struts2-core-2.1.8.1.jar,xwork-core-2.1.6.jar
1.2 将struts.xml文件拷贝到项目的src目录下
1.3 修改web.xml文件
添加:
<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>
2.action中方法的调用方式
2.1 自动方法调用(只能调用execute)
2.2 指定方法调用(通过设置action标签中的method属性)
2.3 动态方法调用(在调用时,在action后加!方法名称,如:login!deletUser)
注意:<constant name="struts.enable.DynamicMethodInvocation" value="true" />
2.4 通配符调用
3. action接收客户端参数的方式
3.1 直接在action中定义参数变量,并生成set和get方法
3.2 定义接收参数的类
注意:都要为action的成员变量提供get和set方法
3.3 让action实现ModelDriven接口,并实现里面的getModel方法
4.获取request,session,application的方式
4.1 用ActionContext获取,实际上获取到的都是Map对象
4.2 用ServletActionContext获取,获取到的是基于Servlet API的对象
4.3 让action实现RequestAware,SessionAware,ApplicationAware接口,并实现里面的方法
5.四种转向
5.1 action转发到页面(默认)
5.2 action重定向到页面 <result type="redirect">
5.3 action转发到action <result type="chain">
<param name="actionName">login</param>
<param name="nameSpace">/login</param>
<param name="method">login</param>
</result>
5.4 action重定向到action <result type="redirectAction">login</result>
第2个回答  2011-04-10
直接将struts需要的jar包放到WEbRoot下的WEB-INF下面的lib包,并加进去就可以了。本回答被提问者采纳
第3个回答  2011-04-12
1添加Struts1
选中项目,右键--->MyEclipse-->Add Struts Catabilities就行了
2添加Struts2
将Struts2的jar包拷贝包WEB-INF下的lib目录就行了
第4个回答  2011-04-10
eclipse——help——SoftWareUpdates 运行向导添加、