深入Struts2的配置文件

SPRING 2.5 + STRUTS2 配置文件 注释备忘 最近接受了一个项目,SPRING 2.5 + STRUTS2 的。之前的项目只有接触过SPRING 1.X 和 STRUTS 1.X 的。 还是先熟悉一下配置文件的好。 SPRING 2.5 的配置果然比 SPRING 1.X / SPRING 2.0 简洁多了。 第一部分:WEB.XML<web-app version="2.4" xmlns="http:// 阅读详情
1.1.    包配置:
Struts2框架中核心组件就是Action、拦截器等,Struts2框架使用包来管理Action和拦截器等。每个包就是多个Action、多个拦截器、多个拦截器引用的集合。
在struts.xml文件中package元素用于定义包配置,每个package元素定义了一个包配置。它的常用属性有:
l name:必填属性,用来指定包的名字。
l extends:可选属性,用来指定该包继承其他包。继承其它包,可以继承其它包中的Action定义、拦截器定义等。
l namespace:可选属性,用来指定该包的命名空间。
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- struts2的action必须放在一个指定的包空间下定义 -->
    <package name="default" extends="struts-default">
    <!-- 定义处理请求URL为login.action的Action -->
        <action name="login" class="org.qiujy.web.struts.action.LoginAction">
        <!-- 定义处理结果字符串和资源之间的映射关系 -->
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>
如上示例的配置,配置了一个名为default的包,该包下定义了一个Action。
1.2.    命名空间配置:
考虑到同一个Web应用中需要同名的Action,Struts2以命名空间的方式来管理Action,同一个命名空间不能有同名的Action。
Struts2通过为包指定namespace属性来为包下面的所有Action指定共同的命名空间。
把上示例的配置改为如下形式:
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- struts2的action必须放在一个指定的包空间下定义 -->
    <package name="qiujy" extends="struts-default">
    <!-- 定义处理请求URL为login.action的Action -->
        <action name="login" class="org.qiujy.web.struts2.action.LoginAction">
        <!-- 定义处理结果字符串和资源之间的映射关系 -->
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
   
    <package name="my" extends="struts-default" namespace="/manage">
    <!-- 定义处理请求URL为login.action的Action -->
        <action name="backLogin" class="org.qiujy.web.struts2.action.LoginAction">
        <!-- 定义处理结果字符串和资源之间的映射关系 -->
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package></struts>
如上配置了两个包:default和my,配置my包时指定了该包的命名空间为/manage。
对于包default:没有指定namespace属性。如果某个包没有指定namespace属性,即该包使用默认的命名空间,默认的命名空间总是""。
对于包my:指定了命名空间/manage,则该包下所有的Action处理的URL应该是“命名空间/Action名”。如上名为backLogin的Action,它处理的URL为:
http://localhost:8080/userlogin_struts2/manage/backLogin.action
Struts2的命名空间的作用等同于struts1里模块的作用。
1.3.    包含配置:
在Struts2中可以将一个配置文件分解成多个配置文件,那么我们必须在struts.xml中包含其他配置文件。
<struts>
    <include file="struts-default.xml"/>
    <include file="struts-user.xml"/>
    <include file="struts-book.xml"/>
    <include file="struts-shoppingCart.xml"/>
   
    ......
   </struts>
1.4.    拦截器配置:
见后面章节介绍。
1.5.    常量配置:
Struts2框架有两个核心配置文件,其中struts.xml文件主要负责管理应用中的Action映射, 及Action处理结果和物理资源之间的映射关系。除此之外,Struts2框架还包含了一个struts.properties文件,该文件主义了Struts2框架的大量常量属性。但通常推荐也是在struts.xml文件中来配置这些常量属性。
如:后面会讲到Struts2的国际化,它的资源文件位置就用常量属性来指定:
<struts>
    ......
    <constant name="struts.custom.i18n.resources" value="messages"/>
</struts>
表示指定了资源文件的放置在classes目录下,基本名是messages,则在classes目录下您就应该放置类似messages_zh_CN.properties,message_en.properties名的文件。
2.    Struts2的Action
2.1.    实现Action类:
Struts2中Action是核心内容,它包含了对用户请求的处理逻辑,我们也称Action为业务控制器。
Struts2中的Action采用了低侵入式的设计,Struts2不要求Action类继承任何的Struts2的基类或实现Struts2接口。(但是,我们为了方便实现Action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并重写此类里的public String execute() throws Exception方法。因为此类中实现了很多的实用接口,提供了很多默认方法,这些默认方法包括获取国际化信息的方法、数据校验的方法、默认的处理用户请求的方法等,这样可以大大的简化Action的开发。)
Struts2中通常直接使用Action来封装HTTP请求参数,因此,Action类里还应该包含与请求参数对应的属性,并且为属性提供对应的getter和setter方法。(当然,Action类中还可以封装处理结果,把处理结果信息当作一属性,提供对应的getter和setter方法)
修改第一部分的用户登录示例:把Action改成如下:
package org.qiujy.web.struts2.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
/**
 *@authorqiujy
 *@version1.0
 */
publicclass LoginAction extends ActionSupport{
    private String userName;
    private String password;
   
    private String msg; //结果信息属性
   
    /**
     *@returnthemsg
     */
    public String getMsg() {
        returnmsg;
    }
    /**
     *@parammsgthemsgtoset
     */
    publicvoid setMsg(String msg) {
        this.msg = msg;
    }
    /**
     *@returntheuserName
     */
    public String getUserName() {
        returnuserName;
    }
    /**
     *@paramuserNametheuserNametoset
     */
    publicvoid setUserName(String userName) {
        this.userName = userName;
    }
    /**
     *@returnthepassword
     */
    public String getPassword() {
        returnpassword;
    }
    /**
     *@parampasswordthepasswordtoset
     */
    publicvoid setPassword(String password) {
        this.password = password;
    }
   
    /**
     *处理用户请求的excute()方法
     *@return结果导航字符串
     *@throwsException
     */
    public String execute() throws Exception{
       if("test".equals(this.userName) &&
"test".equals(this.password)){
           msg = "登录成功,欢迎" + this.userName;
           returnthis.SUCCESS;
       }else{
           msg = "登录失败,用户名或密码错";
           returnthis.ERROR;
       }
    }
}
往success.jsp和error.jsp页面中添加  EL表达式来显示结果信息。则最终效果跟以前一样。
2.2.    Action访问Servlet API:
Struts2中的Action并没有和任何Servlet API耦合,这样框架更具灵活性,更易测试。
但是,对于web应用的控制器而言,不访问Servlet API几乎是不可能的,例如跟踪HTTP Session状态等。Struts2框架提供了一种更轻松的方式来访问Servlet API。Struts2中提供了一个ActionContext类(当前Action的上下文对象),通过这个类可以访问Servlet API。下面是该类中提供的几个常用方法:
l public static ActionContext getContext() :获得当前Action的ActionContext实例。
l public Object get(Object key) :此方法类似于调用HttpServletRequest的getAttribute(String name)方法。
l public void put(Object key, Object value) :此方法类似于调用HttpServletRequest 的setAttribute(String name, Object o)。
l public Map getParameters() :获取所有的请求参数。类似于调用HttpServletRequest对象的getParameterMap() 方法。
l public Map getSession() :返回一个Map对象,该Map对象模拟了HttpSession实例。
l public void setSession(Map session) : 直接传入一个Map实例,将该Map实例里的key-value对转换成session的属性名-属性值对。
l public Map getApplication() :返回一个Map对象,该对象模拟了该应用的ServletContext实例。
l public void setApplication(Map application) :直接传入一个Map实例,将该Map实例里的key-value对转换成application的属性名-属性值对。
修改以上用户登录验证示例的Action类中的execute方法:
public String execute() throws Exception{
        if("test".equals(this.userName) && "test".equals(this.password)){
            msg = "登录成功,欢迎" + this.userName;
            //获取ActionContext实例,通过它来访问Servlet API
            ActionContext context = ActionContext.getContext();
            //看session中是否已经存放了用户名,如果存放了:说明已经登录了;
//否则说明是第一次登录成功
            if(null != context.getSession().get("uName")){
                msg = this.userName + ":你已经登录过了!!!";
            }else{
                context.getSession().put("uName", this.userName);
            }
           
            returnthis.SUCCESS;
        }else{
            msg = "登录失败,用户名或密码错";
            returnthis.ERROR;
        }
    }
       Struts2中通过ActionContext来访问Servlet API,让Action彻底从Servlet API 中分离出来,最大的好处就是可以脱离Web容器测试Action。
       另外,Struts2中还提供了一个ServletActionContext类,Action只要继承自该类,就可以直接访问Servlet API。具体方法参看struts2的API文档。
3.    一个Action内包含多个请求处理方法的处理
Struts1提供了DispatchAction,从而允许一个Action内包含多个请求处理方法。Struts2也提供了类似的功能。处理方式主要有以下三种方式:
3.1.    动态方法调用:
DMI:Dynamic Method Invocation 动态方法调用。
动态方法调用是指:表单元素的action不直接等于某个Action的名字,而是以如下形式来指定对应的动作名:
<form method="post" action="userOpt!login.action">
则用户的请求将提交到名为”userOpt”的Action实例,Action实例将调用名为”login”方法来处理请求。同时login方法的签名也是跟execute()一样,即为public String login() throws Exception。
注意:要使用动态方法调用,必须设置Struts2允许动态方法调用,通过设置struts.enable.DynamicMethodInvocation常量来完成,该常量属性的默认值是true。
3.1.1.      示例:
修改用户登录验证示例,多增加一个注册用户功能。
1.       修改Action类:
package org.qiujy.web.struts2.action;
 
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
 
/**
 *@authorqiujy
 *@version1.0
 */
publicclass LoginAction extends ActionSupport{
    private String userName;
    private String password;
   
    private String msg; //结果信息属性
   
    /**
     *@returnthemsg
     */
    public String getMsg() {
        returnmsg;
    }
    /**
     *@parammsgthemsgtoset
     */
    publicvoid setMsg(String msg) {
        this.msg = msg;
    }
    /**
     *@returntheuserName
     */
    public String getUserName() {
        returnuserName;
    }
    /**
     *@paramuserNametheuserNametoset
     */
    publicvoid setUserName(String userName) {
        this.userName = userName;
    }
    /**
     *@returnthepassword
     */
    public String getPassword() {
        returnpassword;
    }
    /**
     *@parampasswordthepasswordtoset
     */
    publicvoid setPassword(String password) {
        this.password = password;
    }
   
    /**
     *处理用户请求的login()方法
     *@return结果导航字符串
     *@throwsException
     */
    public String login() throws Exception{
        if("test".equals(this.userName) && "test".equals(this.password)){
            msg = "登录成功,欢迎" + this.userName;
            //获取ActionContext实例,通过它来访问Servlet API
            ActionContext context = ActionContext.getContext();
            //看session中是否已经存放了用户名,如果存放了:说明已经登录了;
//否则说明是第一次登录成功
            if(null != context.getSession().get("uName")){
                msg = this.userName + ":你已经登录过了!!!";
            }else{
                context.getSession().put("uName", this.userName);
            }
           
            returnthis.SUCCESS;
        }else{
            msg = "登录失败,用户名或密码错";
            returnthis.ERROR;
        }
    }
   
    public String regist() throws Exception{
        //将用户名,密码添加到数据库中
        //...
        msg = "注册成功。";
        returnthis.SUCCESS;
    }
}
 
2.       struts.xml文件:没有什么变化,跟以前一样配置
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="my" extends="struts-default" namespace="/manage">
    <!-- 定义处理请求URL为login.action的Action -->
        <action name="userOpt" class="org.qiujy.web.struts2.action.LoginAction">
        <!-- 定义处理结果字符串和资源之间的映射关系 -->
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>
3.       页面:
index.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
 <head>
    <title>用户登录页面</title>
 </head>
 
 <body>
  <h2>用户入口</h2>
  <hr>
    <form action="manage/userOpt!login.action" method="post">
    <table border="1">
         <tr>
             <td>用户名:</td>
             <td><input type="text" name="userName"/></td>
         </tr>
         <tr>
             <td>密码:</td>
             <td><input type="password" name="password"/></td>
         </tr>
         <tr>
             <td colspan="2">
                 <input type="submit" value=" 确定 "/>
             </td>
         </tr>
    </table>
    </form>
 </body>
</html>
regist.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
 <head>
    <title>用户注册页面</title>
 </head>
 
 <body>
  <h2>用户注册</h2>
  <hr>
    <form action="manage/userOpt!regist.action" method="post">
    <table border="1">
         <tr>
             <td>用户名:</td>
             <td><input type="text" name="userName"/></td>
         </tr>
         <tr>
             <td>密码:</td>
             <td><input type="password" name="password"/></td>
         </tr>
         <tr>
             <td colspan="2">
                 <input type="submit" value=" 注册 "/>
             </td>
         </tr>
    </table>
    </form>
 </body>
</html>
4.       运行结果:
ollama接入图像识别大模型 阅读详情

相关推荐

WPF基础系列六:MVVM框架

MVVM框架 文章目录MVVM框架前言一、MVVM框架简介1. 什么是MVVM?2. MVVM的优势3.MVVM的应用场景二、MVVM使用及示例 前言 在WPF开发中,经典的编程模式是MVVM,是为WPF量身定做的模式,该模式充分利用了WPF的数据绑定机制,最大限度地降低了Xmal文件和CS文件的耦合度,也就是UI显示和逻辑代码的耦合度,如需要更换界面时,逻辑代码修改很少,甚至不用修改。 与WinForm开发相比,我们一般在后置代码中会使用控件的名字来操作控件的属性来更新UI,而在WPF中通常是通过

iceberg7012的博客 2万+

深入Struts2

深入Struts2                                                  ++YONG原创,转载请注明 1.    深入Struts2的配置文件 本部分主要介绍struts.xml的常用配置。 1.1.    包配置: Struts2框架中核心组件就是Action、拦截器等,Struts2框架使用包来管理Action和拦截器等。每个包就是多个Action...

++yong 126

Claude Code vs Codex实战对比:哪个更适合你的Next.js项目?(含真实案例)

本文通过两个真实的Next.js项目案例,深度对比了Claude Code与Codex在UI实现和业务逻辑构建中的表现。Claude Code在代码质量、设计还原度和架构完整性方面优势明显,适合对可维护性要求高的生产级项目;而Codex则以快速响应和低成本见长,更适合原型开发或内部工具。文章提供了基于项目类型、团队能力和成本效益的详细选择策略。

weixin_29201859的博客 817

struts.xml mysql_struts.xml配置文件详解

struts.xml 是 Struts2 框架的核心配置文件,该文件主要用于配置 Action 和请求的对应关系,以及配置逻辑视图和物理视图(逻辑视图就是在 struts.xml 文件中配置的 元素,它的 name 属性值就是逻辑视图名;物理视图是指 元素中配置的结果页面,如 JSP 文件)资源的对应关系。struts.xml 文件通常放在 Web 应用的 WEB-INF/classes 目录...

weixin_31233537的博客 375

BeikeShop跨境电商独立站系统V1.5.0免授权全开源版

BeikeShop 一款开源好用的跨境电商系统,BeikeShop 是基于 Laravel 开发的一款开源商城系统,主要面向外贸/跨境电商行业提供商品管理、订单管理、会员管理、支付、物流、系统管理等功能。BeikeShop商城 Beike多语言商城 多货币商城B2C商城系统 代码100%开源 PHP商城系统 H5/PC商城 跨境电商系统 跨境商城系统 电商商城系统 Laravel 9 框架插件化开发,MVC架构 支持插件市场。Event 机制实现Hook功能,自定义可视化装修。

827

经典蓝牙中的Sniff、Hold与Park模式

若主设备在Sniff Attempt窗口外发送数据,从设备无法接收,需依赖重传机制。移动时立即传输数据。:Sniff Interval=20ms,Attempt=1时隙。:传感器每2秒唤醒一次同步信标,主设备按需唤醒特定传感器采集数据。Slave进入Sniff模式:每20ms唤醒1个时隙监听数据。:设备需要短暂执行高优先级任务(如扫描其他蓝牙设备)。:无数据传输超时后,设备可能自动进入Sniff模式。:主设备通过信标唤醒从设备,或从设备主动请求退出(:平衡功耗与响应速度,适合间歇性数据传输设备。

shishuangquan111的博客 1683

【struts2】struts2配置文件—struts.xml

解决在断网环境下,配置文件无提示的问题: 我们可以看到Struts.xml在断网的情况下,前面有一个叹号,这时,我们按alt+/ 没有提示,这是因为” http://struts.apache.org/dtds/struts-2.0.dtd”是一个网络地址,如果上网的话,IDE会自动帮我们下载此文件,如果断网就没有办法了,但是我们还是有解决方法的. 首先在源码包里找到struts-2.0.dt...

lmb55的专栏 1074

企业知识库AI助手的权限管理架构:如何实现细粒度控制?

在展开架构设计前,我们需要先明确“细粒度权限”的核心定义,并理解它对AI助手的特殊意义。权限管理的“粒度”,本质是权限控制的精准程度。维度粗粒度权限细粒度权限资源范围entire document(整个文档)field/paragraph(字段/段落)操作类型read/write(读/写)generate_summary/download/export(生成摘要/下载/导出)上下文约束无(静态权限)time/ip/device(时间/IP/设备)适用场景简单系统(如内部wiki)

大数据洞察的博客 1049

Struts2配置文件

Struts2配置详解1、配置文件的加载顺序1.1、加载时机1.2、加载顺序1.3、default.properties的默认配置1.4、修改struts2的访问后缀1.5、配置struts2的开发模式2、配置文件的主要元素2.1、package元素2.2、action元素2.3、result元素 1、配置文件的加载顺序 1.1、加载时机 struts2有很多配置文件,如下面的表格: 当应用被tomcat加载的时候,struts2的配置文件就已经被加载过了。 1.2、加载顺序 顺序 配置文件名

爱心家园的博客 668

YOLO26优化:注意力魔改 | 用于微小目标检测的上下文增强和特征细化网络ContextAggregation,助力小目标检测,暴力涨点

用于微小目标检测的上下文增强和特征细化网络ContextAggregation,助力小目标检测

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 498

【75】OpenCV C++实战篇——OpenCV 图像拼接、全景拼接(教程合集)

本文整理了OpenCV实现图像拼接的技术教程合集,涵盖C++和Python两种语言实现方案。图像拼接技术旨在将多张重叠图像融合为无缝全景图,既扩大视角又保持高分辨率。内容包含水平矫正等关键步骤(附对比效果图),提供多个实战案例的完整代码和详细讲解,涉及单应性矩阵、图像变换等核心技术。不同案例展示了从两张到多张图像的拼接效果,适合不同层次的学习者参考实践。

程序员,他们想的是什么?他们想的永远都是技术,他们崇尚的也永远都是技术。 532

Struts2 配置文件

Struts2 应用程序所需的基本配置,包括配置一些重要的配置文件:web.xml、struts.xml、struts-config.xml以及struts.properties。 在个人实际项目中使用web.xml和struts.xml这两个配置文件来运行项目,不过为了了解更多,还是再来说明一下其他的文件。 1. web.xml文件 web.xml配置文件是一种J2EE配置文件,决定ser...

jcs1992的博客 245

基于STM32设计的智能水产养殖系统(2025)_359

本设计提出并实现了一种基于STM32F103RCT6微控制器的智能水产养殖控制系统,针对传统水产养殖中人工依赖高、环境参数监测滞后、控制手段单一等问题,集成了TDS水质检测、水温检测、水位监测、自动喂食、自动充氧、换水控制与加热保温等功能。系统采用ESP8266模块实现WIFI联网,通过MQTT协议将关键数据上传至华为云物联网平台,支持Android手机APP和Windows上位机远程监测与控制。同时,系统具备本地LCD数据显示与蜂鸣器异常报警功能,支持本地按键控制操作。整套系统功能完善,响应灵敏,运行稳定

637

Struts2的配置文件

Struts2共有5类配置文件,分别是struts.xml、struts.properties、Web.xml、struts-plugin.xml和struts_default.xml。 1、struts.xml文件       struts.xml定义应用本身使用的action映射及result,但我们一般将应用的各个模块分到不同的配置文件中。在拦截器中,必须在使用前进行注册,struts配

模块思维 1997

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

struts2配置文件

<?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...

weiwjacsdn的博客 147

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

struts2_关键的配置文件

环境:IntelliJ IDEA 2018.1.4 x64 包括了web.xml、struts.xml、struts-config.xml以及struts.properties。 web.xml(web/WEB-INF) 部署描述符,是一种J2EE配置文件,决定servlet容器的HTTP元素需求如何进行处理。它严格来说不是一个Struts2 配置文件,但它是Struts2 运作所需要进行配...

Onion的博客 198

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

struts2配置文件的加载

首先来看一下web.xml中的struts2的过滤器<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter>

renzhengxiao的博客 394

Struts 2 –包含多个Struts配置文件

Struts 2带有“ 包含文件 ”功能,可以将多个Struts配置文件包含到一个单元中。 单个Struts配置文件 让我们看一个糟糕的Struts 2配置示例。 struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Found...

一名可爱的技术搬运工 246

struts2核心配置文件名称及加载

默认情况下,Struts2的配置文件名称为struts.xml,且该文件放在src根目录下。项目加载时会自动加载该struts.xml,如下图所示: 如果需要修改struts.xml的位置,例如把struts.xml放到struts2文件夹下,结构如下图所示,该怎么办呢? Struts2在web.xml中的一般配置如下: [html] view pla

lyf_ldh的博客 2869

struts2 的配置文件DTD

高手可以跳过。。。。 最近因项目需开始重新使用Struts2,很久没用过了,所以花了点时间去重新学习,无论是spring还是hibernate,都是通过加载配置文件来实现功能。所以这里我就先开始掌握STRUTS的配置文件。 好记性不如烂笔头,再说拉写在论坛里面可能对大伙有点用处,还可以给自己加分。。。何乐而不为呢! 关于DTD(Document Type definition), 大家可以参考这里...

u011124352的博客 249

Struts2的配置文件详解

Struts2的配置文件 最重要的是Struts.xml和Struts.propertes 主要介绍struts.xml 在struts.xml中,定义了包括action,每个action调用的拦截器,以及每个action的可能结果 struts2的配置文件支持继承,struts2-core-VERSION.jar文件中包含了默认的配置文件。struts-default.xml

oldbig_lin的博客 1万+

struts2的配置文件

struts2的配置文件 类型 struts2共有4类配置文件,     struts.properties:定义框架自身的全局变量。     struts-default.xml:定义框架自身使用的action映射及result定义。     struts-plugin.xml:struts插件使用的配置文件,比如当使用struts和spring结合时就需要在web.xml中引用该配置

System_Mush的专栏 767

Struts2配置文件之struts-default.xml

<!-- /* * $Id: struts-default.xml 559615 2007-07-25 21:25:25Z apetrelli $ * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE f

xyls_2009的专栏 729

Struts2 配置文件struts.xml示范模板

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">

虽然我很菜 4944

Struts2的配置文件的分割

配置文件的分割: 原来Struts2只有一个配置文件,就算新建几个配置文件,项目运行时也只会运行struts.xml,所以就需要分割配置文件 第一步: 新建4个配置文件,可以更多 struts.xml 名字必须一模一样(因为项目运行默认执行struts.xml) global.xml 这是一个项目全局错误的映射配置文件(也可以不用叫这个名字) 其他的就是自定义命名 第二步:操作global.x...

qq_44424498的博客 229

Struts2中的 配置文件

struts2中涉及到的配置文件有: web.xml、struts.xml、struts.properties、default.properties、struts-default.xml web.xml Struts2与Web应用的整合需要借助于web.xml,通常,所有的MVC框架都需要Web应用加载一个核心控制器。struts2的核心控制器设计成一个Filter,为了让W

My_de_Dream的专栏 1051
上一篇: 深入Struts2的配置文件
下一篇: SQL查询艺术(T-SQL)[节选]SQL调优之大事务分段处理
cpp_lzth
博客等级 码龄19年 125粉丝 63原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址