04. Spring开发的基本配置

设置pom.xml依赖,添加spring-mvc依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bailiang</groupId>
    <artifactId>myshop</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <spring-version>4.3.24.RELEASE</spring-version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.26</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
        </dependency>
    </dependencies>
</project>

设置web.xml

添加Spring-context配置文件路径和监听器,用于Spring自动创建ApplicationContext对象

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-context*.xml</param-value>
    </context-param>
<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

添加Spring过滤器,用于解决中文显示问题

<filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
</filter>
<filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

配置Spring的核心分发器:DispatcherServlet

<servlet>
    <servlet-name>springServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 添加一个spring-mvc.xml文件,用于配置SpringMVC -->
        <param-value>classpath*:/spring-mvc*.xml</param-value>
    </init-param>
    <!-- 启动优先级 指定Spring的Servlet最先启动,否则有jvm决定启动顺序 -->
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

设置spring-mvc.xml

注意添加xmlns:mvc命名空间和命名空间路径

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
       ">
    <!-- 默认的注解映射的支持件 -->
    <mvc:annotation-driven />
    <!-- 加载本配置文件中属性值的配置文件,这里属性的配置值保存在myshop.properties文件中 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:myshop.properties" />
    <!-- 设置组件扫描过滤器,只扫描包"com.bailiang.myshop"下的组件 -->
    <context:component-scan base-package="com.bailiang.myshop" use-default-filters="false">
        <!-- 设置扫描规则:include(包含)的过滤器是扫描Annotion(注解),这里只扫描@Controller注解 -->
        <!-- 也可以设置exclude(排除),这里没有用到 -->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- 定义视图文件解析器,把逻辑视图路径解析成实际的视图文件 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 设置逻辑视图解析为实际视频文件时添加的前缀、后缀 -->
        <!-- value的值引用自myshop.properties中的设置 -->
        <property name="prefix" value="${web.view.prefix}" />
        <property name="suffix" value="${web.view.suffix}" />
    </bean>
    <!-- 静态资源映射 -->
    <!-- /*是路径匹配,/**是全路径匹配 -->
    <!-- 如目录结构:/a/b/c/d,则/*,只能匹配/a,而/**可以匹配/a、/a/b、/a/b/c、 /a/b/c/d -->
    <mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
</beans>

添加myshop.properties文件并添加配置

#设置逻辑视图的前缀,一般是视图的路径
web.view.prefix=/WEB-INF/views/
#设置逻辑视图的后缀,一般是真实视图文件的扩展名
web.view.suffix=.jsp

修改spring-context.xml配置,去除重复配置

原配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       ">
    <!-- 添加注解支持 -->
    <context:annotation-config />
    <!-- 添加注解扫描的包 -->
    <context:component-scan base-package="com.bailiang.myshop"/>
</beans>

因为在spring-mvc.xml中添加了对 @Controller注解的支持,所以在spring-context.xml可以去掉该支持,修改如下:

<!-- 添加注解扫描的包 -->
<context:component-scan base-package="com.bailiang.myshop">
    <!-- exlude(去除)对@Controller注解的支持 -->
    <context:exlude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component>

最终的目录结构

SpringMVC的Maven目录结构

### 回答1: 可以回答这个问题,它属于技术领域。您可以在配置文件中设置spring.datasource.url属性来指定要连接的数据库URL。例如,如果您正在使用MySQL数据库,则可以将spring.datasource.url设置为jdbc:mysql://localhost:3306/mydatabase。 ### 回答2: spring.datasource.url用于配置数据库的连接地址。它是Spring框架中连接数据库的一个重要属性。在配置文件中,我们可以通过设置它的值来指定要连接的数据库的地址。 在URL中,通常会包含一些必要的信息,如数据库类型、服务器地址、端口号、数据库名称等。不同的数据库有不同的URL格式。例如,对于MySQL数据库,URL的格式可能是jdbc:mysql://localhost:3306/database_name,其中"jdbc:mysql://"代表驱动程序的类型和协议,"localhost"代表数据库所在的服务器地址,"3306"代表数据库服务器的端口号,"database_name"代表数据库的名称。 除了基本的数据库连接信息,URL还可以包含其他的配置项,如用户名和密码。在使用Spring框架进行数据库连接时,我们可以将这些敏感信息设置为配置文件中的属性,并在URL中引用它们。这是为了避免将敏感信息直接暴露在配置文件中,增加数据库的安全性。 通过对spring.datasource.url的正确配置,我们可以成功连接到数据库,并进行数据的读取、写入和更新等操作。对于不同的数据库,我们需要根据其要求正确设置URL的值,以保证数据库连接的成功。 综上所述,spring.datasource.url是用于配置数据库连接地址的重要属性。通过正确设置该属性的值,我们可以成功连接到指定的数据库,并进行相应的操作。 ### 回答3: spring.datasource.url配置用于指定数据库的连接地址。在使用Spring框架开发时,通常需要连接数据库来存储和读取数据。而spring.datasource.url配置就是用来指定数据库的连接地址。 连接地址的格式通常为:jdbc:mysql://host:port/database 其中,host是数据库服务器的地址,可以是IP地址或域名;port是数据库服务器的端口号,默认为3306;database是要连接的具体数据库的名称。 在配置文件中,可以通过设置spring.datasource.url属性来定义连接地址。比如: spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase 上述配置指定要连接的数据库位于本地主机上,端口为3306,数据库名称为mydatabase。 此外,连接地址还可以指定其他的配置信息,如用户名、密码、字符集等。例如: spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=utf8&user=root&password=123456 上述配置中,通过添加参数useUnicode=true和characterEncoding=utf8来指定使用Unicode字符集,并且通过参数user和password指定数据库登录的用户名和密码分别为root和123456。 总之,spring.datasource.url配置是用来指定数据库连接地址的,其格式为jdbc:mysql://host:port/database,可以在其中添加其他参数进行详细配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值