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

Spring -- 集成web环境 基础环境搭建

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

Spring -- 集成web环境 基础环境搭建

基础环境搭建完成后:集成web环境

1. 基础环境搭建

准备工作:导入相关依赖

    
        
        
            junit
            junit
            4.12
            test
        
        
        
            org.springframework
            spring-context
            5.0.5.RELEASE
        
        
        
            org.springframework
            spring-test
            5.0.5.RELEASE
        
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        

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

1.1 创建一个新的项目 添加web支持


1.2 完成dao层和service层的编写

UserDaoImpl.java

package com.tian.dao.impl;


import com.tian.dao.UserDao;

public class UserDaoImpl implements UserDao {
    public void save() {
        System.out.println("save running....");
    }
}

UserServiceImpl.java

package com.tian.service.impl;


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

public class UserServiceImpl implements UserService {

    private UserDao userDao;

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

    public void save() {
        userDao.save();
    }
}

1.3 新建applicationContext.xml管理bean


applicationContext.xml




    
    

    
    
        
    


1.4 添加web层

UserServlet.java

package com.tian.web;


import com.tian.service.impl.UserServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserServiceImpl bean = context.getBean(UserServiceImpl.class);
        bean.save();
    }

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

1.5 配置Tomcat服务器


1.6 配置Servlet访问路径


web.xml



    
        UserServlet
        com.tian.web.UserServlet
    
    
        UserServlet
        /userServlet
    



1.7 启动服务器测试




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

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

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