ssh框架搭建

 1,添加spring支持
2,添加hibernate支持
3,添加struts支持
4,在web.xml里添加监听器。作用是在系统启动时执行该Listener,以读取Spring的xml配置文件。
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext*.xml</param-value>
  </context-param>
5,在web.xml里添加上下文参数,该参数指定了哪些SpringXML配置文件要被加载
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
*以上两步是struts与spring的松耦合方式。可以使用以下片段去调用spring的bean对象
  WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationConte(servletContext);//获得引用
  UserBean userBean=(UserBean)ctx.getBean("userBean");
*以下是struts与spring更深入的紧耦合配置
6,在struts-config.xml里配置ContextLoaderPlugin插件,来加载Spring配置(作用和在web.xml里配置一样)
 
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
      value="/WEB-INF/action-servlet.xml,/WEB-INF/applicationContext.xml"/>
</plug-in>

7,在struts-config.xml里配置DelegatingRequestProcessor,需要重载<controller>元素的processorClass属性
  <controller>
   <set-property property="processorClass"
      value="org.springframework.web.struts.DelegatingRequestProcessor"/>
  </controller>
*如果已有一个自定义的RequestProcessor,这样就不能再使用DelegatingRequestProcessor了。此时可以指定action的type是DelegatingActionProxy
<action path="/user" type="org.springframework.web.struts.DelegatingActionProxy"
    name="userForm" scope="request" validate="false" parameter="method">
  <forward name="list" path="/userList.jsp"/>
  <forward name="edit" path="/userForm.jsp"/>
</action>

第三种struts+spring的方式:继承Spring的ActionSupport类,包括ActionSupport,DispatchAcitonSupport等
public class UserAction extends DispatchActionSupport {

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("entering 'delete' method...");
        }
        WebApplicationContext ctx = getWebApplicationContext();
        UserManager mgr = (UserManager) ctx.getBean("userManager");
        // talk to manager for business logic
        return mapping.findForward("success");
    }
}

8,新建action的时候要在spring的配置文件里配置bean。
<bean name="/user" scope="prototype" autowire="byName"
    class="org.example.web.UserAction"/>
9,依赖注入可以使用设值注入:
public class BuildAction extends DispatchAction {


 private BuildBiz buildBiz=null;
 public void setBuildBiz(BuildBiz buildBiz){
  this.buildBiz=buildBiz;
 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值