Spring集成Web环境

29 篇文章 0 订阅
21 篇文章 0 订阅

添加坐标

 <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.2.1</version>
            <scope>provided</scope>
        </dependency>

搭建环境

第一步 下载tomcat

直接官网下载,解压,打开conf目录下的logging.properties
在这里插入图片描述
将编码改为GBK,解决乱码问题
在这里插入图片描述

第二步 在IDEA中配置Tomcat

编辑配置
在这里插入图片描述
点击+号,选择本地Tomcat
在这里插入图片描述
在name中设置你服务器的名字
在Configure中选择你刚刚安装的Tomcat
在JRE中选择你的JRE
添加端口号
在这里插入图片描述
接下来配置Deployment,点击+。选择Artifcat
在这里插入图片描述
选择你要部署服务器的项目
在这里插入图片描述
最后点击Apply,OK就可以可以

获取应用上下文对象
在这里插入图片描述
在这里插入图片描述
导入坐标

 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>

获取对象

 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 = app.getBean(UserService.class);
        userService.save();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值