struts2整合spring环境配置

上篇文章详细记录了struts2环境配置的步骤,此篇文章主要记录strtus2整合spring环境配置

  1. 添加包
  2. web.xml中相关配置
  3. 创建spring配置文件
  4. 编写测试类

1.添加包(并build path)

             包链接:

2.在web.xml中设置监听器,并指明随后创建的spring的配置文件的地址



	 
    index.html
    index.htm
    jsp/index.jsp
    default.html
    default.htm
    default.jsp
  
	
	
        struts2
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    

    
        struts2
        /*
        
    
    
       contextConfigLocation
   		/WEB-INF/applicationContext.xml
    
    
    
       
           org.springframework.web.context.ContextLoaderListener
       
   

3.spring的配置文件(applicationContext.xml在WEB-INF目录下)





    
       
    
    

在applicationContext.xml注册bean

 

4.service类

package com.adu.service;

public class TestService {
	 public void service() throws Exception {
	       System.out.println("this is service function ");

	    }
}

并在aciton中调用

package com.adu.action;
import com.adu.service.TestService;
import com.opensymphony.xwork2.Action;
public class TestAction implements Action {
	private TestService testService;
	 public TestService getTestService() {
		return testService;
	}
	public void setTestService(TestService testService) {
		this.testService = testService;
	}
	public String execute() throws Exception {
	       System.out.println("execute Struts2 Action ......  ");
		   testService.service(); 
	       return  "ok";

	    }

}

struts.xml配置




    
        
            jsp/first.jsp
        
        
            jsp/first.jsp
        
    

 

你可能感兴趣的:(web开发)