Studs MVC Framework-----PHP的Struts1.X实现

本文介绍了一款基于Jakarta Struts结构的PHP开发框架——PHPStuds,详细展示了其核心配置文件、工作流程及示例程序,帮助读者理解该框架的设计理念与实现方式。

Jakarta Struts结构移植过来的PHP开发框架,使用面向对象的开发结构和API,模拟了一个HTTP Servlet容器和使用了PHP服务页面引擎技术。

下载地址:http://mojavelinux.com/projects/studs/

文件结构如下:

如上图,WEB-INF中的文件都是Studs框架的核心文件(尤其是lib和tld下的文件),其中:

1.messages.properties文件:

welcome.title = Studs :: Welcome
welcome.heading
= Welcome to Studs!
welcome.message
= The application has been successfully installed!

pageviews.message
= This page has been viewed <strong>{ 0 }</strong> times.

2.struts-config.xml文件:

<? xml version="1.0" encoding="ISO-8859-1" ?>
<! DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
    "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"
>
<!--
     This is a basic Studs/Struts configuration file with an example
     welcome action/page and other commented sample elements.
-->
< struts-config >

    
<!--  example configuration of a database connection
    <data-sources>
        <data-source type="horizon.sql.BasicDataSource">
            <set-property
                property="description"
                value="Primary data source for localhost database"/>
            <set-property
                property="driverClassName"
                value="horizon.sql.drivers.MySQLDriver"/>
            <set-property
                property="username"
                value="user"/>
            <set-property
                property="password"
                value="secret"/>
            <set-property
                property="url"
                value="mysql://localhost/dbname"/>
        </data-source>
    </data-sources>
    
-->

    
< form-beans >
        
<!--  sample form bean descriptor for an ActionForm
        <form-bean
            name="inputForm"
            type="app.InputForm"/>
        
-->
    
</ form-beans >

    
< global-exceptions >
        
<!--  sample exception handler
        <exception
            key="expired.password"
            type="app.ExpiredPasswordException"
            path="/changePassword.jsp"/>
        
-->
    
</ global-exceptions >

    
< global-forwards >
        
<!--  Default forward to "welcome" action  -->
        
< forward  name ="welcome"  path ="/welcome.do" />
    
</ global-forwards >

    
< action-mappings >
        
<!--  Default "welcome" action  -->
        
< action  path ="/welcome"  forward ="/pages/welcome.psp" />
        
< action  path ="/example"  forward ="/pages/example.psp" />

        
<!--  sample input and input submit actions

        <action
            path="/Input"
            type="org.apache.struts.actions.ForwardAction"
            parameter="/pages/Input.jsp"/>

        <action
            path="/InputSubmit"
            type="app.InputAction"
            name="inputForm"
            scope="request"
            validate="true"
            input="/pages/Input.jsp"/>

            <action
                path="/edit*"
                type="app.Edit{1}Action"
                name="inputForm"
                scope="request"
                validate="true"
                input="/pages/Edit{1}.jsp"/>

            
-->
    
</ action-mappings >

    
< controller  locale ="false"  inputForward ="true" />

    
< message-resources  parameter ="/WEB-INF/messages.properties" />

</ struts-config >

3.web.xml文件:

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
< web-app >

    
< display-name > Studs Basic Application </ display-name >
    
< description > A bare-bones foundation for creating a web application based on the Studs MVC Framework+ </ description >
    
< context-param >
        
<!--  if controlAllResources is set, all files should be handled by the stratus
             container, including all non-php/html files 
-->
        
< param-name > controlAllResources </ param-name >
        
< param-value > false </ param-value >
    
</ context-param >

    
<!--  Servlet Definitions  -->
    
< servlet >
        
< servlet-name > default </ servlet-name >
        
< servlet-class > stratus.servlets.DefaultServlet </ servlet-class >
        
< init-param >
            
< param-name > listings </ param-name >
            
< param-value > true </ param-value >
        
</ init-param >
        
< load-on-startup > 1 </ load-on-startup >
    
</ servlet >
    
< servlet >
        
< servlet-name > phase </ servlet-name >
        
< servlet-class > phase.servlet.PhaseServlet </ servlet-class >
        
< init-param >
            
< param-name > scratchdir </ param-name >
            
< param-value > /WEB-INF/work </ param-value >
        
</ init-param >
        
< init-param >
            
< param-name > tldresourcedir </ param-name >
            
< param-value > /WEB-INF/tld </ param-value >
        
</ init-param >
        
< init-param >
            
< param-name > ignoreEL </ param-name >
            
< param-value > false </ param-value >
        
</ init-param >
        
< load-on-startup > 2 </ load-on-startup >
    
</ servlet >
    
< servlet >
        
< servlet-name > action </ servlet-name >
        
< servlet-class > studs.action.ActionServlet </ servlet-class >
        
< init-param >
            
< param-name > config </ param-name >
            
< param-value > /WEB-INF/struts-config.xml </ param-value >
        
</ init-param >
        
< init-param >
            
< param-name > validating </ param-name >
            
< param-value > true </ param-value >
        
</ init-param >
        
< load-on-startup > 3 </ load-on-startup >
    
</ servlet >

    
<!--  Servlet Mappings  -->
    
< servlet-mapping >
        
< servlet-name > default </ servlet-name >
        
< url-pattern > / </ url-pattern >
    
</ servlet-mapping >
    
< servlet-mapping >
        
< servlet-name > phase </ servlet-name >
        
< url-pattern > *.psp </ url-pattern >
    
</ servlet-mapping >
    
< servlet-mapping >
        
< servlet-name > action </ servlet-name >
        
< url-pattern > *.do </ url-pattern >
    
</ servlet-mapping >

    
<!--  Mime Type Mappings  -->
    
< mime-mapping >
        
< extension > html </ extension >
        
< mime-type > text/html </ mime-type >
    
</ mime-mapping >
    
< mime-mapping >
        
< extension > css </ extension >
        
< mime-type > text/css </ mime-type >
    
</ mime-mapping >
    
< mime-mapping >
        
< extension > js </ extension >
        
< mime-type > text/javascript </ mime-type >
    
</ mime-mapping >
    
< mime-mapping >
        
< extension > png </ extension >
        
< mime-type > image/png </ mime-type >
    
</ mime-mapping >
    
< mime-mapping >
        
< extension > jpg </ extension >
        
< mime-type > image/jpeg </ mime-type >
    
</ mime-mapping >
    
< mime-mapping >
        
< extension > jpeg </ extension >
        
< mime-type > image/jpeg </ mime-type >
    
</ mime-mapping >
    
< mime-mapping >
        
< extension > gif </ extension >
        
< mime-type > image/gif </ mime-type >
    
</ mime-mapping >

    
<!--  Default Welcome File List  -->
    
< welcome-file-list >
        
< welcome-file > index.psp </ welcome-file >
    
</ welcome-file-list >

</ web-app >

4.文件build.xml:

<? xml version="1.0" ?>
< project  name ="Studs Basic"  basedir ="../.."  default ="package" >
    
< property  file ="../../build.properties"   />

    
< target  name ="package"  depends ="prepare" >
        
< mkdir  dir ="../../${release.dir}/${project.release.version}" />
        
< tar  destfile ="../../${release.dir}/${project.release.version}/studs-basic-${project.release.version}.tar.gz"  compression ="gzip" >
            
< tarfileset  dir ="."  prefix ="studs-basic"  defaultexcludes ="yes" >
                
< exclude  name ="classes/build.xml" />
                
< exclude  name ="**/TODO" />
                
< exclude  name ="**/NOTES" />
            
</ tarfileset >
        
</ tar >
    
</ target >

    
< target  name ="clean" >
        
<!--  make sure to break symlinks  -->
        
< symlink  action ="delete"  link ="${basedir}/WEB-INF/lib"  failonerror ="no" />
        
< symlink  action ="delete"  link ="${basedir}/WEB-INF/tld"  failonerror ="no" />

        
< delete  includeemptydirs ="yes"  failonerror ="no" >
            
< fileset  dir ="WEB-INF/work" >
                
< include  name ="**" />
            
</ fileset >
            
< fileset  dir ="WEB-INF/lib" >
                
< include  name ="**" />
            
</ fileset >
            
< fileset  dir ="WEB-INF/tld" >
                
< include  name ="**" />
            
</ fileset >
        
</ delete >
    
</ target >

    
< target  name ="prepare"  depends ="clean" >
        
<!--  create work directory  -->
        
< mkdir  dir ="WEB-INF/work" />
        
< chmod  dir ="WEB-INF/work"  perm ="0777"  type ="dir" />
        
<!--  create lib directory and copy framework libraries  -->
        
< mkdir  dir ="WEB-INF/lib" />
        
< copy  todir ="WEB-INF/lib" >
            
< fileset  dir ="../../src" >
                
< include  name ="**" />
            
</ fileset >
        
</ copy >
        
<!--  create tld directory and copy known tlds  -->
        
< mkdir  dir ="WEB-INF/tld" />
        
< copy  todir ="WEB-INF/tld" >
            
< fileset  dir ="../../conf/tld" >
                
< include  name ="*.tld" />
            
</ fileset >
        
</ copy >
        
<!--  copy over the controller and server conf file  -->
        
< copy  todir ="." >
            
< fileset  dir ="../../conf" >
                
< include  name ="index.php" />
                
< include  name =".htaccess" />
            
</ fileset >
        
</ copy >
    
</ target >
</ project >

5.logging.properties文件:

==  Setup log appenders  ==
#logging.rootLogger
= DEBUG ,  file
#logging.appender.file
= horizon.util.logging.FileLogAppender
#logging.appender.file.file
= /tmp/studs-basic.log
==  Specific logging per category  ==
#logging.logger.
[ partial package or classname ] = DEBUG

其他文件基本上都可以在框架源代码中找到。

页面文件如下:

1.index.php:

<? php
error_reporting ( E_ALL );
ini_set ( ' include_path ' ,   ' WEB-INF/lib '   .  (DIRECTORY_SEPARATOR  ==   ' / '   ?   ' : '   :   ' ; ' .   ' WEB-INF/classes ' );
umask ( 0002 );

require_once   ' horizon/init.php ' ;

import(
' stratus.connector.HttpProcessor ' );
import(
' stratus.config.ContextConfig ' );

$config   =   &   new  ContextConfig( dirname ( __FILE__ ));
$processor   =   &   new  HttpProcessor( $config -> getContext());
$processor -> run();
?>

2.index.psp:

<% @ page language="php"  %>
<% @ taglib uri="/WEB-INF/tld/phase-core.tld" prefix="c"  %>
<% @ taglib uri="/WEB-INF/tld/studs-html.tld" prefix="html"  %>
< c:redirect  context ="/" >< html:rewrite  forward ="welcome" /></ c:redirect >

3.welcome.psp:

<% @ page language="php"  %>
<% @ taglib uri="/WEB-INF/tld/phase-core.tld" prefix="c"  %>
<% @ taglib uri="/WEB-INF/tld/phase-fmt.tld" prefix="fmt"  %>
<% @ taglib uri="/WEB-INF/tld/studs-html.tld" prefix="html"  %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>
< html  xmlns ="http://www.w3.org/1999/xhtml" >
    
< head >
        
< title >< fmt:message  key ="welcome.title"   /></ title >
        
< meta  http-equiv ="Content-Type"  content ="text/html; charset=UTF-8"   />
    
</ head >
    
< body >
        
< h3 >< fmt:message  key ="welcome.heading"   /></ h3 >
        
< p >< fmt:message  key ="welcome.message"   /></ p >
        
< p >< html:link  action ="/example" > Click here </ html:link >  for an example of the template tag library. </ p >
        
< c:set  var ="welcomePageViewCount"  value ="${applicationScope.welcomePageViewCount + 1}"  scope ="application" />
        
< style ="font-size: small;" >< fmt:message  key ="pageviews.message"  arg0 ="${applicationScope.welcomePageViewCount}" /></ p >
    
</ body >
</ html >

4.example.psp:

<% @ taglib uri="/WEB-INF/tld/studs-template.tld" prefix="template"  %>
<% @ taglib uri="/WEB-INF/tld/phase-string.tld" prefix="str"  %>
<% @ taglib uri="/WEB-INF/tld/phase-core.tld" prefix="c"  %>
< template:insert  template ="/pages/tmpl/template.psp" >
    
< template:put  name ="title"  content ="Studs :: Template Taglib Example"  direct ="true" />
    
< template:put  name ="heading"  direct ="true" > Template Taglib Example </ template:put >
    
< template:put  name ="intro"  direct ="true" >
This page demonstrates the use of the studs template taglib.  Content is inserted by the parent page either directly or via an include file.  Below are two examples of content insertion.
    
</ template:put >
    
< template:put  name ="content"  direct ="true" >
< str:replace  replace ="LF"  with ="&lt;br /&gt;" > This text was specified inline using the template:put tag.

It also uses the str:replace taglib to convert endlines to BR tags.

I am also testing the conditional EL syntax.  
< strong > ${2 % 2 ? 'odd' : 'even'} </ strong ></ str:replace >
    
</ template:put >
    
< template:put  name ="content2"  content ="/pages/tmpl/content.psp"   />
</ template:insert >

5.template.psp:

<% @ taglib uri="/WEB-INF/tld/studs-html.tld" prefix="html"  %>
<% @ taglib uri="/WEB-INF/tld/studs-template.tld" prefix="template"  %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>
< html  xmlns ="http://www.w3.org/1999/xhtml"  xml:lang ="en"  lang ="en" >
    
< head >
        
< title >< template:get  name ="title"   /></ title >
    
</ head >
    
< body >
        
< h3 >< template:get  name ="heading"   /></ h3 >
        
< p >< template:get  name ="intro"   /></ p >
        
< style ="border: 1px dashed #999999; background-color: #F0F0F0;" >
        
< template:get  name ="content"   />
        
</ p >
        
< style ="border: 1px dashed #999999; background-color: #F0F0F0;" >
        
< template:get  name ="content2"   />
        
</ p >
        
< p > &laquo;   < html:link  forward ="welcome" > Back </ html:link ></ p >
    
</ body >
</ html >

6.content.psp:

This text comes from an include file using the template:put tag.

这样,一个基于PHP Studs框架的示例程序就完成了。(注:上诉代码取自studs自带的例子)

【复现】并_离网风光互补制氢合成氨系统容量-调度优化分析(Python代码实现)内容概要:本文围绕“并_离网风光互补制氢合成氨系统容量-调度优化分析”的主题,提供了基于Python代码实现的技术研究与复现方法。通过构建风能、太阳能互补的可再生能源系统模型,结合电解水制氢与合成氨工艺流程,对系统的容量配置与运行调度进行联合优化分析。利用优化算法求解系统在不同运行模式下的最优容量配比和调度策略,兼顾经济性、能效性和稳定性,适用于并网与离网两种场景。文中强调通过代码实践完成系统建模、约束设定、目标函数设计及求解过程,帮助读者掌握综合能源系统优化的核心方法。; 适合人群:具备一定Python编程基础和能源系统背景的研究生、科研人员及工程技术人员,尤其适合从事可再生能源、氢能、综合能源系统优化等相关领域的从业者;; 使用场景及目标:①用于教学与科研中对风光制氢合成氨系统的建模与优化训练;②支撑实际项目中对多能互补系统容量规划与调度策略的设计与验证;③帮助理解优化算法在能源系统中的应用逻辑与实现路径;; 阅读建议:建议读者结合文中提供的Python代码进行逐模块调试与运行,配合文档说明深入理解模型构建细节,重点关注目标函数设计、约束条件设置及求解器调用方式,同时可对比Matlab版本实现以拓宽工具应用视野。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值