Struts + Spring整合3种方法亲身实践(1)

 
Struts + Spring整合3种方法亲身实践(1)
 
由于最近要做一个项目,用到struts, spring, hibernate;之前我做项目,只用过struts ,hibernate(很落后的表现,呵呵!),所以呢下定决心,练了一把,先写一个最简单的struts程序,输入页面:input.do, 一个文本框,输入信息后,点确确定,跳转到另一页面,显示welcome,**,够简单了吧!为了开发简便快捷,我是在eclipse+myeclipse下完成的,下面我把代码贴出,再加点解释:
Struts-config.xml
< struts-config >
  < data-sources />
  < form-beans >
    < form-bean name = "helloWorld" type = "com.yourcompany.struts.form.HelloWorldForm" />
</ form-beans >
< action-mappings >
    < action
      attribute = "helloWorld"
      name = "helloWorld"
      path = "/helloWorld"
      scope = "request"
      type = "com.yourcompany.struts.action.HelloWorldAction" >
      < forward name = "show" path = "/show.jsp" />
    </ action >
    < action forward = "/input.jsp" path = "/input" />
</ action-mappings >
< message-resources parameter = "ApplicationResources" />
</ struts-config >
很简单的一个结构,一个input.do进入的输入页面input.jsp,击点确定,跳转到helloworld.do,经他处理后,跳转到show, that’s OK!
 
web.xml
<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" version = "2.4" xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
  < servlet >
    < servlet-name > action </ servlet-name >
    < servlet-class > org.apache.struts.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 > debug </ param-name >
      < param-value > 3 </ param-value >
    </ init-param >
    < init-param >
      < param-name > detail </ param-name >
      < param-value > 3 </ param-value >
    </ init-param >
    < load-on-startup > 0 </ load-on-startup >
  </ servlet >
  < servlet-mapping >
    < servlet-name > action </ servlet-name >
    < url-pattern > *.do </ url-pattern >
  </ servlet-mapping >
  < jsp-config >
   < taglib >
       < taglib-uri > /tags/struts-bean </ taglib-uri >
       < taglib-location > /WEB-INF/struts-bean.tld </ taglib-location >
   </ taglib >
   < taglib >
       < taglib-uri > /tags/struts-html </ taglib-uri >
       < taglib-location > /WEB-INF/struts-html.tld </ taglib-location >
   </ taglib >
  </ jsp-config >
</ web-app >
 
HelloWorldAction :
package com.yourcompany.struts.action;
//节省长度,导入的类就不贴了
public class HelloWorldAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
           HttpServletRequest request, HttpServletResponse response) {
       HelloWorldForm helloworld=(HelloWorldForm)form;
       request.setAttribute( "helloWorld" , helloworld.getMsg());
       return mapping.findForward( "show" );
    }
}
HelloWorldForm :
package com.yourcompany.struts.form;
public class HelloWorldForm extends ActionForm {
    private static final long serialVersionUID = 1L;
    private String msg ;
    public ActionErrors validate(ActionMapping mapping,
           HttpServletRequest request) {
       return null ;
    }
public void reset(ActionMapping mapping, HttpServletRequest request) {
    }
    public String getMsg() {
       return msg ;
    }
    public void setMsg(String msg) {
       this . msg = msg;
    }
}
 
input.jsp
<%@ page language = "java" pageEncoding = "ISO-8859-1" %>
<%@ taglib uri = "/tags/struts-bean" prefix = "bean" %>
<%@ taglib uri = "/tags/struts-html" prefix = "html" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html:html lang = "true" >
  < head >
    < html:base />
    < title >< bean:message key = "title_input" /></ title >
  </ head >
  < body >
    < html:form  action = "/helloWorld" method = "POST" >
       < bean:message key = "welcome" />< html:text property = "msg" />
       < html:submit > sumbit </ html:submit >
    </ html:form >
  </ body >
</ html:html >
 
show.jsp
<%@ page language = "java" pageEncoding = "ISO-8859-1" %>
<%@ taglib uri = "/tags/struts-bean" prefix = "bean" %>
<%@ taglib uri = "/tags/struts-html" prefix = "html" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html:html lang = "true" >
  < head >
    < html:base />
    < title >< bean:message key = "title_show" /></ title >
  </ head >  
  < body >
    < bean:message key = "input" /> "${helloWorld}" < br />
  </ body >
</ html:html >
 
无论哪种方法都要在 struts-config.xml 里注册一下 Spring 插件 :
struts-config.xml 中加上 : ( 红色为加入部分 )
  < message-resources parameter = "ApplicationResources" />
 <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> 
       <set-property property="contextConfigLocation"     value="/WEB-INF/applicationContext.xml" /> 
 </plug-in>
</ struts-config >
为了体现 Spring IOC, 我们再加多几个类 :
一个接口:
package com.yourcompany.struts.action;
import com.yourcompany.struts.form.HelloWorldForm;
public interface HelloWorldService {
    public abstract String addMesg(HelloWorldForm helloworld);
}
 
实现这个接口的类:
package com.yourcompany.struts.action;
import com.yourcompany.struts.form.HelloWorldForm;
public class HelloWorldServiceImpl implements HelloWorldService{
 
    public String addMesg(HelloWorldForm helloworld) {
       return helloworld.getMsg();
    }
 
}
 
Spring 的配置文件 : ( 位置 /WEB-INF/applicationContext.xml )
<? xml version = "1.0" encoding = "UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
    < bean id = "helloWorldService" class = "com.yourcompany.struts.action.HelloWorldServiceImpl" ></ bean >
</ beans >
准备工作完成 , 下一步开始整合
 
 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值