锘??xml version="1.0" encoding="utf-8" standalone="yes"?> ------------------------------------------- ------------------------------------------ For all those out there using Spring together with Struts, i created
a small howTo regarding automatic generation of relevant files,
especially the boring action-servlet-xml file which must be in synch
with your struts-config.xml, at least for all Struts actions which
should be injected by Spring. Lets start with the Action class: ClientManager clientManagerService; /** With this tags, the ant build target outlined below will create a
struts-config.xml and the necessary action-servlet.xml needed by
Spring. Lets see how the target looks: <fileset dir="c:/xdoclet-1.2.2" includes="*.jar"/> <taskdef name="springdoclet" <target name="myTest"> <springdoclet destDir="c:\" verbose="true"> Be sure to modify the classpath definition and the destDir values of
springdoclet and webdoclet to suit your needs. Right now the Spring
task only searches for Actions in order to create action-servlet.xml,
if you also want to create your normal service beans like
ClientManagerService, you should create another springdoclet task and
output things to applicationContext.xml if you like. But this is mostly users choice, as you know, Spring supports from
one to many bean definition files and its up to you how you want to
have your spring xml world. This will be created when you run the mentioned target: (action-servlet.xml) <beans <bean <property name="clientManagerService"> <ref bean="clientManagerService"/> </bean> <!-- <!DOCTYPE struts-config PUBLIC "-//Apache Software
Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <!-- ========== Data Sources Definitions =================================== --> <!-- ========== Form Bean Definitions =================================== --> <!-- <!-- ========== Global Exceptions Definitions =================================== --> <!-- ========== Global Forward Definitions =================================== --> <!-- ========== Action Mapping Definitions =================================== --> <!-- If you have non XDoclet actions, define them in a file
called struts-actions.xml and place it in your merge directory. --> <!-- Define your Struts controller in a file called struts-controller.xml and place it in your merge directory. --> <!-- Define your Struts message-resources in a file called
struts-message-resources.xml and place it in your merge directory.
--> <!-- Define your Struts plugins in a file called struts-plugins.xml and place it in your merge directory. -->
XDoclet鐨刡uild.xml鍐欐硶
錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳錛濓紳
---------------------------------------------
/**
* Action to delete a Client
* Date: 09.02.2005
* Time: 15:29:15
*
* @author Logemann - Logentis e.K. (ml@logentis.de)
* @version $Id$
* @struts.action path="clientDelete" validate="false"
* type="org.springframework.web.struts.DelegatingActionProxy"
* name="emptyform"
* @struts.action-forward name="back"
* path="/clientManager.html" redirect="true"
* @spring.bean name="clientDelete"
*/
public class ClientDeleteAction extends Action {
* Spring injection
*
* @param clientManagerService clientManagerService
* @spring.property ref="clientManagerService"
*/
public void setClientManagerService(ClientManager clientManagerService) {
this.clientManagerService = clientManagerService;
}
[..]
<path id="classpath">
<fileset dir="c:/j2sdkee1.3.1/lib" includes="*.jar"/>
<fileset dir="c:/struts/lib" includes="struts.jar"/>
</path>
classname="xdoclet.modules.spring.SpringDocletTask"
classpathref="classpath"/>
<taskdef name="webdoclet"
classname="xdoclet.modules.web.WebDocletTask"
classpathref="classpath"/>
<webdoclet destDir="c:\" force="true" verbose="true">
<fileset dir="${src.dir}">
<include name="**/*Form.java" />
<include name="**/*Action.java" />
<include name="**/*Servlet.java" />
</fileset>
<strutsconfigxml version="1.1" validateXml="true"/>
</webdoclet>
<fileset dir="${src.dir}">
<include name="**/*Action.java" />
</fileset>
<springxml destinationFile="action-servlet.xml"/>
</springdoclet>
</target>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
default-autowire="no"
default-lazy-init="false"
default-dependency-check="none"
>
name="clientDelete"
class="de.logentis.versysng.action.clientmanager.ClientDeleteAction"
>
</property>
To include additional bean definitions for Spring in the generated
application context file, add a file to your XDoclet merge directory
called spring-beans.xml that contains the <bean></bean> markup.
-->
</beans>
(struts-config.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Define your Struts data sources in a file called struts-data-sources.xml and place
it in your merge directory.
-->
<form-beans>
If you have non XDoclet forms, define them in a file called struts-forms.xml and
place it in your merge directory.
-->
</form-beans>
<!--
Define your exceptions in a file called global-exceptions.xml and place
it in your merge directory.
-->
<!--
Define your forwards in a file called global-forwards.xml and place
it in your merge directory.
-->
<action-mappings>
<action
path="clientDelete"
type="org.springframework.web.struts.DelegatingActionProxy"
name="emptyform"
scope="request"
unknown="false"
validate="false"
>
<forward
name="back"
path="/clientManager.html"
redirect="true"
/>
</action>
</action-mappings>
You may be wondering why the taskdef for the schema update tool is inside our schema target, rather than at the top of the build file, next to the one for hbm2java. Well, I wanted it up there too, but I ran into a snag that's worth explaining. I got strange error messages the first time I tried to build the schema target, complaining there was no hibernate.properties on the class path and our compiled Track class couldn't be found. When I ran it again, it worked. Some detective work using ant -verbose revealed that if the classes directory didn't exist when the taskdef was encountered, Ant helpfully removed it from the class path. Since a taskdef can't have its own dependencies, the solution is to move it into the schema target, giving it the benefit of that target's dependencies, ensuring the classes directory exists by the time the taskdef is processed.
涔熻浣犱細濂囨紝涓轟綍鎵句簡schema鏇存柊宸ュ叿鐨則askdef浼氬湪schema鐨則arget涓繘琛屽畾涔夛紝鑰屼笉鏄湪build鏂囦歡鐨勯《閮紝绱ф帴鐫hbm2java榪涜瀹氫箟銆傚垰寮濮嬫垜涔熸槸閭f牱鎯崇殑錛岃繖閲屾垜闇瑕佽В閲婁竴涓嬭繖閲岀殑闂銆傚綋絎竴嬈℃瀯寤簊chema鏃訛紝鎴戝緱鍒頒簡璁稿鑾悕濂囧鐨勯敊璇紝鎶ラ敊淇℃伅鏄細鍦╟lasspath涓壘涓嶅埌hibernate.properties鍜孴rack綾匯備絾鏄啀嬈℃瀯寤虹殑鏃跺欙紝灝卞彲浠ヤ簡銆傚綋浣跨敤鍛戒護鈥渁nt -verbose鈥濇椂錛屼綘灝卞彲浠ュ彂鐜板叾涓殑緇嗚妭涔嬫墍鍦ㄣ傚鏋滃湪ant瑙f瀽鍒皌askdef鏃訛紝build鏂囦歡涓墍浣跨敤鐨刢lass鐩綍騫朵笉瀛樺湪錛岄偅涔坅nt灝變細鑷繁灝嗘鐩綍浠巆lasspath涓Щ闄ゃ備絾鏄痶askdef鍙堟棤娉曞畾涔夎嚜宸辯殑dependencies錛屽洜姝よ繖閲屼笉寰椾笉灝嗗叾縐誨叆鍒皊chema target涓紝榪欐牱涔熷氨鍙互浣跨敤鍒皌arget鐨刣ependencies錛屼繚璇佸湪ant澶勭悊taskdef鐨勬椂鍊欙紝class鐩綍蹇呯劧宸茬粡瀛樺湪銆?/P>