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

Posted Z && Y

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring -- 集成web环境 基础环境搭建相关的知识,希望对你有一定的参考价值。

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

1. 基础环境搭建

准备工作:导入相关依赖

    <dependencies>
        <!--        junit单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--        spring context-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--        spring集成junit-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!-- servlet依赖 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- JSP依赖 -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--配置Dao-->
    <bean id="userDao" class="com.tian.dao.impl.UserDaoImpl"/>

    <!--配置service-->
    <bean id="userService" class="com.tian.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>
</beans>

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.tian.web.UserServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/userServlet</url-pattern>
    </servlet-mapping>

</web-app>

1.7 启动服务器测试




以上是关于Spring -- 集成web环境 基础环境搭建的主要内容,如果未能解决你的问题,请参考以下文章

Spring集成web环境-基本三层架构环境搭建

Spring集成web环境-基本三层架构环境搭建

Spring集成web环境-基本三层架构环境搭建

Spring - Spring集成web环境 -基本三层架构搭建案例

Spring与Web环境集成

Spring集成web环境-ContextLoaderListener监听器的分析