栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring集成Web环境

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring集成Web环境

Spring整合Web开发需要导入的坐标


        UTF-8
        11
        11
    

        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    11
                
            
        
    


        
            javax.servlet
            servlet-api
            3.1.0
        


        
            org.springframework
            spring-context
            4.2.4.RELEASE
        

        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        

        
            Maven_Repository.javax.servlet.jsp
            javax.servlet.jsp-api
            2.3.1
            provided
        

        
            org.springframework
            spring-web
            5.0.5.RELEASE
        
        
            Maven_Repository.com.h2database
            h2
            1.3.158
        


    

① 配置ContextLoaderListener监听器
② 使用WebApplicationContextUtils获得应用上下文

获取ApplicationContext对象是从servletContext域中获取的,还有就是使用WebApplicationContextUtils获取app。 可以直接从Spring获取app对象,省去了自己创建。还有就是以后要使用到多次app对象,所以就是省去了new出多了app对象。

dao层代码

package com.itheima.dao.impl;

import com.itheima.dao.UserDao;

public class UserDaoImpl implements UserDao {

    public void save() {
        System.out.println("save is running");
    }
}

service层代码

package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.service.UserService;

public class UserServiceImpl implements UserService {

    private UserDao userDao;
    
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void save() {

        userDao.save();

    }
}

web层

package com.itheima.web;

import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/servlet")
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");

        ServletContext servletContext = this.getServletContext();

//         ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");

//        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);

        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);

        UserService userService = (UserService) app.getBean("userService");

        userService.save();

    }
}

applicationContext.xml





    

    
        
    


web.xml




    
    
        contextConfigLocation
        classpath:applicationContext.xml
    

    
        org.springframework.web.context.ContextLoaderListener
    


问题:

在配置maven时候,project下面有红色波浪线的话,可能是复制的坐标,需要重写写一遍,还有就是是maven可能是3.6.3版本,idea和maven可能会出现冲突,所以要降低maven版本,改为3.6.1版本即可。

在部署tomcat的时候,可能回出现监听器的问题,如果是tomcat10,就需要降低tomcat版本,如果是tomcat8.5.5及其一下的版本,就需要做一下操作。




就会出现lib包,再重新部署一下项目就可以了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/731459.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号