第一个SpringBoot HelloWorld

  • SpringBoot内置集成Tomcat,不需要启动服务器,直接启动App类即可运行项目。
  • static下面放图片、js文件、css文件等。
  • application.properties配置文件

    # 不用输入项目名直接访问,因为默认值为/,改为如下则输入localhost:8080/HelloWorld
    server.context-path=/HelloWorld
    
  • 绑定配置文件中的属性

    @Value("${helloWorld}") //绑定application.properties文件该属性
    private String helloWorld;
    

    将配置文件中的属性封装成对象

  • application.properties

    mysql.jdbcName=com.mysql.jdbc.Driver
    mysql.dbUtil=jdbc:mysql://localhost:3306/db_book
    mysql.userName=root
    mysql.password=123456
    
  • 写一个配置类

    @Component //交给Spring容器搞成bean
    @ConfigurationProperties(prefix="mysql") //到application.properties中找到前缀为mysql的属性,自动将属性封装成对象
    public class MysqlConfiguration {
    
        private String jdbcName;
        private String dbUtil;
        private String userName;
        private String password;
    
        getter、setter
    }
    
  • 在需要用到的地方注入

    //不过要是MySQL配置也像上面这么绑那就太累了,可以将属性封装成对象再注入到这里
    @Resource
    private MysqlConfiguration mysqlConfiguration;
    
  • 默认单例模式,在不同的类里用注解注入的同类的bean都是同一个。
  • 代码实例:D:/ideaPro

    使用freemarker

  • 配置maven

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    
  • 编写controller(免后缀)

    @RequestMapping("/say")
    public ModelAndView say(){
        ModelAndView mav=new ModelAndView();
        mav.addObject("message","SpringBoot大爷你好!");
        mav.setViewName("helloWorld"); //免后缀
            return mav;
    }
    
  • 编写相应freemarker(可以先建HTML文件然后改后缀为ftl。这种文件的优点就在于访问路径不用加后缀,其他跟HTML没差。记得要写在resources/templates下才能通过上面路径访问到)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        show:${message} <!-- 还是这样获取数据 -->
    </body>
    </html>
    
  • 下面代码是简化的取集合

    <#list bookList as book> <!--把集合里取出来的个体放到book中 -->
    <tr>
        <td>${book.id}</td>
        <td>${book.name}</td>
        <td>${book.author}</td>
        <td>
            <a href="/book/preUpdate/${book.id}">修改</a>
            <a href="/book/delete?id=${book.id}">删除</a>
        </td>
    </tr>
    </#list> <!--这里应该会飘红,但是注意到文件本身不飘红,所以没事-->
    
  • 代码实例:D:/ideaPro

    springMVC整合freemaker

  • spring-mvc.xml

    <!-- freeMarker视图解析 17061434 -->
    <bean id="freeMarkerViewResolver"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="prefix" value=""/>
        <property name="suffix" value=""/>
        <property name="order" value="2"/>
        <property name="viewNames" value="*.ftl"/>
        <property name="contentType" value="text/html;charset=utf-8"/>
    </bean>
    <!-- ftl配置 -->
    <bean id="freeMarkerConfigurer"
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
    </bean>
    
    <!-- 后面这个不知道需不需要 -->
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1"/>
        <property name="mediaTypes">
            <map>
                <entry key="json" value="application/json"/>
                <entry key="jsonp" value="application/javascript"/>
            </map>
        </property>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
                <ref bean="freeMarkerViewResolver"/>
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
            </list>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
                </bean>
                <bean class="com.suning.viewsolver.JsonpView"/>
            </list>
        </property>
        <property name="ignoreAcceptHeader" value="true"/>
    </bean>
    

@RestController支持ajax请求

  • 写一个页面,在该页面实现ajax请求,比如index.html(放在static下直接访问就行了)

    <head>
    <!-- ajax请求 -->
    <script src="http://www.java1234.com/jquery-easyui-1.3.3/jquery.min.js"></script>
    <script type="text/javascript">
    
        function show(){
            $.post("ajax/hello",{},function(result){
            alert(result);
            });
        }
    
    </script>
    </head>
    <body>
        <button onclick="show()">你大爷</button>
    </body>
    
  • 写一个controller类处理ajax请求

    @RestController
    @RequestMapping("/ajax")
    public class HelloWorldAjaxController {
    
        @RequestMapping("/hello")
        public String say(){
            //返回json字符串
            return "{'message1':'SpringBoot大爷你好','message2':'Spring大爷你好2'}";
        }
    }
    
  • 以上操作结果为,在首页点击“你大爷”后弹窗显示{‘message1’:’SpringBoot大爷你好’,’message2’:’Spring大爷你好2’}
  • 代码实例:D:/ideaPro