spring和struts的区别 实现一个简单的struts和spring框架
实现一个简单的struts和spring框架
apache终于发布了struts 的正式版 struts GA 所以我也该从webwork迁移至struts struts 基本上就是webwork的翻版 所以迁移过程倒是很简单 只需要修改下配置文件和一些包名就可以了 如果在Eclipse Netbeans这些集成开发工具的帮助下 记不清包名也很容易找到想要的类的 呵呵 在Eclipse下建立一个Dynamic Web Application 从struts 的lib目录中复制下面的库文件到WEB INF/lib目录下 mons logging jarfreemarker jarognl jarstruts api jarstruts core jarstruts spring plugin jarxwork jar从spring中lib目录中复制下面的库文件到WEB INF/lib目录下 spring jar修改web xml 增加一个struts的分派器filter 映射所有的url pattern 再增加一个spring的ContextLoaderListener监听器 修改后的内容如下 xml 代码 <?xml version= encoding= UTF ?> <web app id= WebApp_ID version= xmlns= xmlns:xsi= instance xsi:schemaLocation= app_ _ xsd > <display name>struts tutorial</display name> <filter> <filter name>struts </filter name> <filter class> apache struts dispatcher FilterDispatcher</filter class> </filter> <filter mapping> <filter name>struts </filter name> <url pattern>* action</url pattern> </filter mapping> <wele file list> <wele file>index jsp</wele file> </wele file list> <listener> <listener class> sprntext ContextLoaderListener</listener class> </listener> </web app>
写一个简单的Action HelloWorld java 代码 package tutorial; import opensymphony xwork ActionSupport; public class HelloWorld extends ActionSupport { public static final String MESSAGE = Struts is up and running ; public String execute() throws Exception { setMessage(MESSAGE); return SUCCESS; } private String message; public void setMessage(String message){ ssage = message; } public String getMessage() { return message; } }

在源文件路径下(项目的src目录)增加struts xml配置action 这个文件是集成spring的关键所在 这里面描述有如何将spring 集成到struts 的相关信息 xml 代码 <!DOCTYPE struts PUBLIC //Apache Sofare Foundation//DTD Struts Configuration //EN dtd > <struts> <! 下面这句表明对象是由spring负责产生的 加上这句后 struts会产生让spring负责 产生bean 如果spring不能产生bean 则由struts自己产生 也可以在struts properties 文件内定义这个属性 > <constant name= objectFactory value= spring ></constant> <package name= struts tutoial extends= struts default namespace= / > <! 注意 现在action的class属性不再是类的名字了 而是在spring中的bean的id 详细信息请看下面的spring的bean配置文件applicationContext xml > <action name= HelloWorld class= helloWorld > <result>/helloWorld jsp</result> </action> <! Add your actions here > </package> </struts>
在WEB INF/目录下增加spring的bean配置文件applicationContext xml xml 代码 <?xml version= encoding= UTF ?> <!DOCTYPE beans PUBLIC //SPRING//DTD BEAN //EN beans dtd > <beans> <bean id= helloWorld class= tutorial HelloWorld ></bean> </beans>
lishixinzhi/Article/program/Java/ky/201311/28344