• 使用Maven搭建SpringMVC项目


    参考文档

    标题:超详细使用Maven 搭建 Spring MVC 本地部署Tomcat 实现 HelloWorld Spring MVC笔记 

    链接:https://www.jianshu.com/p/a6fd2a37104c 

    spring常见配置

    使用Maven搭建SpringMVC项目 

     

    注意:

    1.配置tomcat问题  https://www.cnblogs.com/l199616j/p/11430615.html

    2.tomcat端口被占用问题 https://blog.csdn.net/hyt182380/article/details/103394377

    3.返回json  https://www.cnblogs.com/zhanglijun/p/9104706.html

    4. spring-servlet.xml中创建注解映射器 创建注解适配器的两种方法

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/mvc 
               http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
                <property name="objectMapper">  
                    <bean class="com.fasterxml.jackson.databind.ObjectMapper">  
                        <property name="dateFormat">  
                            <bean class="java.text.SimpleDateFormat">  
                                <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />  
                            </bean>  
                        </property>  
                    </bean>  
                </property>  
            </bean>  
        </mvc:message-converters>
    </mvc:annotation-driven>
        
        
        
        
    <!-- 创建注解映射器 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
    <!-- 创建注解适配器 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <!-- 配置json格式支持 -->
        <property name="messageConverters">
            <list>
               <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                 <property name="supportedMediaTypes">
                     <list>
                         <value>text/plain;charset=UTF-8</value>
                         <value>text/html;charset=UTF-8</value>
                     </list>
                 </property>
               </bean>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
                   <property name="objectMapper">  
                       <bean class="com.fasterxml.jackson.databind.ObjectMapper">  
                           <property name="dateFormat">  
                               <bean class="java.text.SimpleDateFormat">  
                                   <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />  
                               </bean>  
                           </property>  
                       </bean>  
                   </property>  
               </bean> 
            </list>
        </property>
    </bean>       

    5. 添加properties文件

    其他方式比较繁琐,可参考:SpringMVC加载配置Properties文件的几种方式

    (1)在spring-mvc.xml加入:
    xmlns:util="http://www.springframework.org/schema/util"
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.0.xsd
    
    <util:properties id="applicationProps" location="classpath:conf/config_development.properties"/>2)在controller中加入:
    @Autowired
    private Properties applicationProps;
    
    public Properties getApplicationProps() {
    return applicationProps;
    }
    
    (3).在controller中利用applicationProps读取配置文件
    String jumpUrl = applicationProps.getProperty("learn.jump.url");

    6. 读取 properties文件和xml文件,

    1) 可以使用java.util.Properties,够强大

          Properties prop = new Properties();
          FileInputStream fis = new FileInputStream("sampleprops.xml");
          prop.loadFromXML(fis);
          或 prop = PropertiesLoaderUtils.loadAllProperties("sampleprops.xml");

     2) 纯文本读取 可参考:java中yml、properties配置文件读取方式

    7. bean的xml格式

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

    也可以

    xmlns:p="http://www.springframework.org/schema/p"

        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
               destroy-method="close"   
               p:driverClassName="${jbdc.driverClassName}"
               p:url="${jdbc.url}"
             p:username="${jbdc.username}"
            p:password="${jbdc.password}"
       />

     

    https://github.com/jiangsiwei2018/BigData.git 实例代码git仓库地址
  • 相关阅读:
    M、V、VM分别代表什么?
    HTML行内元素、块状元素、行内块状元素的区别
    野指针与僵尸对象
    mybatis 引入java 常量
    mybatis 分页默认500条
    java 引用
    java 查看类大小
    java 排序多个条件
    java for循环批次处理
    Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
  • 原文地址:https://www.cnblogs.com/satansz/p/15769565.html
Copyright © 2020-2023  润新知