spring 项目配置中一些问题和使用方法

/ 和 /*的区别
/ servlet 中会覆盖servlet 中默认的拦截
spring 需要单独处理  css js 等内容

/*"可以匹配所有url,包括带扩展名的,一般只用在过滤器上

spring 配置jsp


<bean id="defaultViewResource" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="suffix" value=".jsp">property>
    <property name="prefix" value="/view/">property>
    <property name="order" value="0">property>
bean>
spring 设置默认页

设置全局的ctx

1.创建一个拦截器

public class BasePathInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        request.setAttribute("ctx",request.getContextPath());
        return true;
    }
}

2.注册拦截器

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <bean class="com.bai.system.main.BasePathInterceptor"></bean>
    </mvc:interceptor>
</mvc:interceptors>
放行静态资源

使用默认 servlet


配置属性文件 properties
<context:property-placeholder location="classpath:*.properties"/>
配置数据库连接
<dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>${project.mysql.version}version>
dependency>
<dependency>
    <groupId>org.mybatisgroupId>
    <artifactId>mybatisartifactId>
    <version>${project.mybatis.version}version>
dependency>
<dependency>
     <groupId>org.mybatisgroupId>
     <artifactId>mybatis-springartifactId>
     <version>${project.mybatis-spring.version}version>
dependency>
<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druidartifactId>
    <version>${project.durid.version}version>
dependency>

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc_url}" />
    <property name="username" value="${jdbc_user}" />
    <property name="password" value="${jdbc_password}" />

    
    <property name="filters" value="stat" />
    
    <property name="maxActive" value="20" />
    <property name="initialSize" value="1" />
    <property name="minIdle" value="1" />
    
    <property name="maxWait" value="60000" />
    
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    
    <property name="minEvictableIdleTimeMillis" value="300000" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
    
    <property name="poolPreparedStatements" value="true" />
    <property name="maxOpenPreparedStatements" value="20" />
bean>
配置mybatis





    
    
    
    
    
    
    
    


    
    

java.io.IOException: Could not find resource mappers/*.xml

有可能是不存在任何的xml文件,可以先把配置注释掉

配置事务

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-txartifactId>
    <version>${project.spring.version}version>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-jdbcartifactId>
    <version>${project.spring.version}version>
dependency>
org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.ArrayList

无法将对象直接转换为json 添加依赖

<dependency>
    <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-databindartifactId>
    <version>${project.jackson.version}version>
dependency>

你可能感兴趣的:(spring,java,后端)