SSH之Spring整合struts2

Struts2+spring整合/result常用类型/拦截器

为什么?通过spring管理Struts2的组件,实现注入

怎么整合?

1.创建项目

2.导包:struts2-spring-plugin struts2-core

 

3.配置文件:web.xml spring-context.xml

web.xml:配置struts2的filert,配置spring的启动加载配置文件,配置spring的监听器

 <!-- 配置spring监听,用于初始化spring容器 -->
  <listener>
          <listener-class>
              org.springframework.web.context.ContextLoaderListener
          </listener-class>
  </listener>
  <!-- 指定spring配置文件的位置 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-*.xml</param-value>
  </context-param>
  <!-- struts2主控制器的配置 -->
  <filter>
      <filter-name>mvc</filter-name>
      <filter-class>
          org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      </filter-class>
  </filter>
  <filter-mapping>
          <filter-name>mvc</filter-name>
          <url-pattern>/*</url-pattern>
  </filter-mapping>

spring-context.xml:配置spring扫描的包

案例:HelloWorld

控制层:

@Controller
//每一个请求都创建一个Action
@Scope("prototype")
public class HelloAction {
    
    @Resource
    private DemoService demoService;
    
    private String message;
    
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String execute() {
        message="你好";
        demoService.hello();
        System.out.println("helloworld");
        return "success";
    }
}

Service层

@Service("demoService")
public class DemoServiceImpl implements DemoService{

    public void hello() {
        System.out.println("Service Hello");
        
    }
    
}

 

posted @ 2019-07-09 21:43  LLLLCCDD  阅读(105)  评论(0编辑  收藏  举报