老王的spring-boot

Spring Boot的Web開發javascript

  • Spring Boot的Web開發支持 Spring Boot提供了spring-boot-starter-web爲Web開發予以支持.它爲咱們提供了嵌入的Tomcat以及Spring MVC的依賴.css

  • Thymeleaf模板引擎 Spring Boot 推薦使用Thymeleaf做爲模板引擎.由於其提供了完整的Spring MVC支持. 由於使用嵌入的Servlet容器來運行JSP的話有一些小問題,內嵌Tomcat,Jetty不支持以jar的形式運行JSP,並且Undertow不支持JSP.html

    • Thymeleaf基礎知識 Thymeleaf是一個java類庫,它是一個xml/xhtml/html5的模板引擎,能夠做爲MVC的Web應用的View層. Thymeleaf基礎知識html5

    • 補充: 在javascript中訪問modeljava

      <script th:inline="javascript">
      	var single=[[${singlePerson}]];
      	console.log(single.name + "/" + single.age);
      	</script>
  • Spring Boot的Thymeleaf支持 Spring Boot經過自動配置功能對Thymeleaf進行了自動配置,所以能夠直接使用.jquery

    <!DOCTYPE html>
    	<html xmlns:th="http://www.thymeleaf.org" lang="en">
    	<head>
    	    <meta charset="UTF-8"/>
    	    <meta http-equiv="X-UA-COMPATIBLE" content="IE=edge"/>
    	    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    	    <title>Thymeleaf</title>
    	    <link th:src="@{bootstrap/css/bootstrap.min.css}" rel="stylesheet"/>
    	    <link th:src="@{bootstrap/css/bootstrap-theme.min.css}" rel="stylesheet"/>
    	    <script th:src="@{js/jquery-2.2.3.js}" type="text/javascript"/>
    	    <script th:src="@{bootstrap/js/bootstrap.min.js}"/>
    	    <script>
    	        $(function () {
    	            console.log(123);
    	        });
    	    </script>
    	</head>
    	<body>
    	<div class="panel panel-primary">
    	    <div class="panel-heading">
    	        <h3 class="panel-title"> 訪問model</h3>
    	    </div>
    	    <div class="panel-body">
    	        <span th:text="${singlePerson.name}"></span>
    	    </div>
    	</div>
    
    	<div th:if="${not #lists.isEmpty(people)}">
    	    <div class="panel panel-primary">
    	        <div class="panel-heading">
    	            <h3 class="panel-title">列表</h3>
    	        </div>
    	        <div class="panel-body">
    	            <ul class="list-group">
    	                <li class="list-group-item" th:each="person:${people}">
    	                    <span th:text="${person.name}"></span>
    	                    <span th:text="${person.age}"></span>
    	                    <button class="btn" th:onclick="'getName(\''+${person.name}+'\');'">得到名字
    	                    </button>
    	                </li>
    	            </ul>
    	        </div>
    	    </div>
    	</div>
    	<script th:inline="javascript">
    	    var single = [[${singlePerson}]];
    	    console.log(single.name + "/" + single.age);
    	    function getName(name) {
    	        console.log(name);
    	    }
    	</script>
    	</body>
    	</html>
    package person.learn.thymeleaf;
    
    	import org.springframework.stereotype.Controller;
    	import org.springframework.ui.Model;
    	import org.springframework.web.bind.annotation.RequestMapping;
    
    	import java.util.ArrayList;
    	import java.util.List;
    
    	/**
    	 * 1.使用Thymeleaf做爲模板引擎,要在application.yml裏設置spring.thymeleaf.caching設置爲false
    	 * 2.用了模板引擎以後,原先對於jsp的設置無效
    	 * 3.(idea編輯器須要手動點擊CTRL+F9,手動編譯,由於IDEA文件不須要手動保存)修改Thymeleaf模板的內容後,要不重啓項目就生效的話,須要make一下,
    	 * 或者使用熱部署:http://mamicode.com/info-detail-1346413.html
    	 * 4.要使用Thymeleaf模板 請將pom.xml中相應的jar依賴註釋去掉
    	 * Created by barton on 16-5-19.
    	 */
    	@Controller
    	public class ThymeleafController {
    
    	    @RequestMapping("/thymeleaf")
    	    public String index(Model model) {
    	        Person single = new Person("aa", 11);
    
    	        List<Person> people = new ArrayList<>();
    
    	        Person p1 = new Person("xx", 11);
    	        Person p2 = new Person("yy", 22);
    	        Person p3 = new Person("zz", 33);
    
    	        people.add(p1);
    	        people.add(p2);
    	        people.add(p3);
    
    	        model.addAttribute("singlePerson", single);
    	        model.addAttribute("people", people);
    
    	        return "thymeleaf/thymeleaf";
    	    }
    	}
    package person.learn.thymeleaf;
    
    	/**
    	 * Created by barton on 16-5-19.
    	 */
    	public class Person {
    
    	    public Person(String name, Integer age) {
    	        this.name = name;
    	        this.age = age;
    	    }
    
    	    private String name;
    	    private Integer age;
    
    	    public String getName() {
    	        return name;
    	    }
    
    	    public void setName(String name) {
    	        this.name = name;
    	    }
    
    	    public Integer getAge() {
    	        return age;
    	    }
    
    	    public void setAge(Integer age) {
    	        this.age = age;
    	    }
    	}
  • Spring Boot自動配置的靜態資源web

    • 類路徑文件 把類路徑下的/static,/public,/resources和/META-INF/resources文件夾下的靜態文件直接映射爲/**,能夠經過http://localhost:8080/**來訪問.
    • webjar webjar就是將咱們經常使用的腳本框架封裝在jar包中的jar包. 把webjar的/META-INF/resources/webjars/下的靜態文件映射爲/webjar/,能夠經過http://localhost:8080/webjar/ 來訪問
  • Spring Boot 對靜態首頁的支持spring

    • classpath:/META-INF/resources/index.html
    • classpath:/resources/index.html
    • classpath:/static/index.html
    • classpath:/public/index.html
  • 將Tomcat替換爲Jettybootstrap

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    
        <!-- 使用jetty替代tomcat,若是是使用jsp做爲模板,則不能使用內嵌的jetty容器。 -->
        <!-- 內嵌的jetty容器不支持jsp模板 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
  • 將Tomcat替換爲Undertowtomcat

    <dependency>
    	         <groupId>org.springframework.boot</groupId>
    	         <artifactId>spring-boot-starter-web</artifactId>
    	         <exclusions>
    	             <exclusion>
    	                 <groupId>org.springframework.boot</groupId>
    	                 <artifactId>spring-boot-starter-tomcat</artifactId>
    	             </exclusion>
    	         </exclusions>
    	     </dependency>
    	<dependency>
    	         <groupId>org.springframework.boot</groupId>
    	         <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>
  • 設置Favicon 只需將本身的favicon.ico 防止在類路徑根目錄,類路徑META-INF/resources/下,類路徑resources/下,類路徑static/下或者類路徑public/下.

相關文章
相關標籤/搜索