dex" path="/WEB-INF/jsp/index.jsp"/>
<forward name="show" path="/WEB-INF/jsp/show.jsp"/>
</action>
<action
path="/input"
type="org.apache.struts.actions.ForwardAction"
parameter="/WEB-INF/jsp/input.jsp"/>
</action-mappings>
<!--注册ContextLoaderPlugIn -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/config.xml" />
</plug-in>
<message-resources parameter="messages"/>
</struts-config>
整合Spring与Struts的几种方法(2)
时间:2011-03-14 Fruitful
Step 2:修改Spring的配置文件config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id=”helloWorldService” class=”com.strutstest.service.impl.HelloWorldServiceImpl”>
</bean>
<!--注意此处的映射必须与Struts中的动作对应-->
<bean name=”/helloWorld” class=”com.strutstest.action.HelloWorldAction”>
<property name=”helloWorldService”>
<ref bean=”helloWorldService”/>
</property>
</bean>
</beans>
Step 3:定义作为Model的Action Form类及相应接口、实现
定义Action Form:
package com.strutstest.action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class HelloWorld extends ActionForm {
private String msg = null;
public void setMsg(String msg) {
this.msg = msg;
}
public String getMsg() {
return this.msg;
}
public void reset(ActionMapping mapping, HttpServletRequest req) {
this.msg = null;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if("".equals(getMsg())) {
errors.add("msg",new ActionError("error"));
}
return errors;
}
}
定义HelloWorld类的接口:
package com.strutstest.service;
import com.strutstest.action.HelloWorld;
public interface HelloWorldService {
public abstract String addMsg(HelloWorld helloWorld);
}
定义接口的实现:
package com.strutstest.service.impl;
import com.strutstest.action.HelloWorld;
import com.strutstest.service.HelloWorldService;
public class HelloWorldServiceImpl implements HelloWorldService {
public String addMsg(HelloWorld helloWorld) {
helloWorld.setMsg("Hello World... " + helloWorld.getMsg());
return helloWorld.getMsg();
}
}
整合Spring与Struts的几种方法(3)
时间:2011-03-14 Fruitful
Step 4:定义Action
package com.strutstest.action;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.s
|