Spring

spring 整合web项目(struts2)

2018-06-07  本文已影响3人  DouDouZH

一、准备工作

1、创建新的web项目
2、导入jar包
3、创建相应的文件
4、配置spring
image.png
5、配置struts2
image.png
6、配置struts2的过滤器
image.png

二、代码

UserDao.java

package work.zhangdoudou.Dao;

public class UserDao {
    public void addUser() {
        System.out.println("dao----------");
    }
}

UserService.java

package work.zhangdoudou.Service;

import work.zhangdoudou.Dao.UserDao;

public class UserService {
    
    UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public void add() {
        System.out.println("service------");
        userDao.addUser();
    }
}

UserAction.java

package work.zhangdoudou.Action;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.opensymphony.xwork2.ActionSupport;
import work.zhangdoudou.Service.UserService;

public class UserAction extends ActionSupport {
    
    @Override
    public String execute() throws Exception {
        System.out.println("Action--------------");
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService=(UserService)context.getBean("userService");
        userService.add();
        
        return NONE;
    }
}

spring配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd
                         http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
        
    <bean id="userService" class="work.zhangdoudou.Service.UserService">
        <property name="userDao" ref="userDao"></property>
    </bean>
    <bean id="userDao" class="work.zhangdoudou.Dao.UserDao"></bean>

</beans>

struts2配置struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="false" value="struts.enable.DynamicMethodInvocation"/>
    <constant value="true" name="struts.devMode"/>
            <!-- 通配符方式实现 -->
            <package name="methoddemo" extends="struts-default" namespace="/">
                <action name="userAction" class="work.zhangdoudou.Action.UserAction">
                    <result name="none">/index.jsp</result>
                </action>
            </package>
</struts>

过滤器web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>spring_day02_2SpringStruts2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

log4j.properties

log4j.rootLogger=info, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
#log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Print the date in ISO 8601 format
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN

三、运行结果

image.png

四、分析改良

1、分析:
2、解决方案

(1)在服务器启动的时候,创建对象加载配置文件
(2)使用底层监听器、servletContext对象

3、spring里面不需要我们自己写代码,已经封装好了

(1)spring封装了一个监听器,只需要配置监听器就可以了
(2)配置监听器之前要导入spring整合web的jar包


jar包
i配置

(3)指定加载spring配置文件的位置

异常

(4)把Action创建交给spring管理

4、改良后的代码

UserDao.java

package work.zhangdoudou.Dao;

public class UserDao {
    public void addUser() {
        System.out.println("dao----------");
    }
}

UserService.java

package work.zhangdoudou.Service;

import work.zhangdoudou.Dao.UserDao;

public class UserService {
    
    UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public void add() {
        System.out.println("service------");
        userDao.addUser();
    }
}

UserAction.java

public class UserAction extends ActionSupport {
    UserService userService;
    @Override
    public String execute() throws Exception {
        System.out.println("Action--------------");
//      ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//      UserService userService=(UserService)context.getBean("userService");
        
        userService.add();
        
        return NONE;
    }
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd
                         http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
        
    <bean id="userService" class="work.zhangdoudou.Service.UserService">
        <property name="userDao" ref="userDao"></property>
    </bean>
    <bean id="userDao" class="work.zhangdoudou.Dao.UserDao"></bean>
    
    <!-- 配置action -->
    <bean id="userAction" class="work.zhangdoudou.Action.UserAction" scope="prototype">
        <!-- 注入service -->
        <property name="userService" ref="userService"></property>
    </bean>
    
</beans>

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="false" value="struts.enable.DynamicMethodInvocation"/>
    <constant value="true" name="struts.devMode"/>
            <!-- 通配符方式实现 -->
            <package name="methoddemo" extends="struts-default" namespace="/">
                <!-- class属性不写全路径 写spring管理的id -->
                <action name="userAction" class="userAction">
                    <result name="none">/index.jsp</result>
                </action>
            </package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>spring_day02_2SpringStruts2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 指定spring配置文件的位置 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
   
  
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

log4j.properties

log4j.rootLogger=info, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
#log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Print the date in ISO 8601 format
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN
5、运行结果
image.png
上一篇下一篇

猜你喜欢

热点阅读