欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring集成Web环境的实例详解

程序员文章站 2021-11-23 10:46:13
spring整合web开发需要导入的坐标 utf-8

spring整合web开发需要导入的坐标

<properties>
        <project.build.sourceencoding>utf-8</project.build.sourceencoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
<build>
        <plugins>
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-compiler-plugin</artifactid>
                <version>3.8.1</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
        </plugins>
    </build>
<dependencies>
<!--        下面是web开发需要的依赖-->
        <dependency>
            <groupid>javax.servlet</groupid>
            <artifactid>servlet-api</artifactid>
            <version>3.1.0</version>
        </dependency>

<!--        下面是spring依赖-->
            <groupid>org.springframework</groupid>
            <artifactid>spring-context</artifactid>
            <version>4.2.4.release</version>
<!--        下面两个是spring整和web需要的坐标-->
            <artifactid>javax.servlet-api</artifactid>
            <scope>provided</scope>
            <groupid>maven_repository.javax.servlet.jsp</groupid>
            <artifactid>javax.servlet.jsp-api</artifactid>
            <version>2.3.1</version>
<!--        下面是上下文应用整和的坐标-->
            <artifactid>spring-web</artifactid>
            <version>5.0.5.release</version>
            <groupid>maven_repository.com.h2database</groupid>
            <artifactid>h2</artifactid>
            <version>1.3.158</version>
    </dependencies>

① 配置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

<?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">

    <bean id="userdao" class="com.itheima.dao.impl.userdaoimpl"></bean>
    <bean id="userservice" class="com.itheima.service.impl.userserviceimpl">
        <property name="userdao" ref="userdao"></property>
    </bean>
</beans>

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">
    <!--全局初始化参数-->
    <context-param>
        <param-name>contextconfiglocation</param-name>
        <param-value>classpath:applicationcontext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
    </listener>
</web-app>

问题:

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

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

Spring集成Web环境的实例详解

Spring集成Web环境的实例详解

Spring集成Web环境的实例详解

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

到此这篇关于spring集成web环境的文章就介绍到这了,更多相关spring集成web环境内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!