How to write spring mvc project, input, and how to run

The first step: to build a spring project

Step two: what works update

The third step: to build a controller class

              

. 1  Package com.baidu.demo;
 2  
. 3  Import org.springframework.stereotype.Controller;
 . 4  Import org.springframework.ui.Model;
 . 5  Import org.springframework.web.bind.annotation.GetMapping;
 . 6  Import org.springframework.web .bind.annotation.RequestParam;
 . 7  
. 8 @Controller // indicates that the class is the controller 
. 9  public  class GreetingController {
 10  
. 11      / ** @RequestParam request for acquiring the parameters corresponding to request.getParameter servlet () method
 12 is        * the Model It is a model for storing data, corresponding to the servlet setAttribute () and getAttribute (). Model which can put POJO
13 is       * / 
14      
15      @GetMapping ( "/ Greeting") // Note @GetMapping represents a get request, corresponds to the servlet doGet () method 
16      public String Greeting (@RequestParam (name = "name", = required to false , = defaultValue "World" ) String name, the Model Model) {
 . 17          model.addAttribute ( "name", name); // the addAttribute () method of the setAttribute === ()
 18 is          
. 19        // return here corresponds to the request servlet . .getRequestDispacher () forword () method, for jumping the page 
20 is          return "Greeting"; // Greeting directed to src / main / resources / templates / greeting.html file, writing suffixes omitted 
21      }
 22 is  
23 is }

Step Four: Download Spring Source

               Website https://spring.io/guides/gs/serving-web-content/

                  

Step five: Copy the source code of static / index.html and templates / greeting.html to the project's main / resources / static / and main / resources / templates / in

              

 1 <!DOCTYPE HTML>
 2 <html>
 3 <head> 
 4     <title>Getting Started: Serving Web Content</title> 
 5     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 6 </head>
 7 <body>
 8        <!-- 跳转到控制器中 -->
 9     <p>Get your greeting <a href="/greeting">here</a></p>
10 </body>
11 </html>
1 <! DOCTYPE HTML>
 2 <- the introduction of property xmlns:! Th = "http://www.thymeleaf.org to start thymeleaf Templates ->
 3 <HTML xmlns: TH =" HTTP: //www.thymeleaf. ORG ">
 . 4 <head> 
 . 5      <title> the Getting Started: the Serving the Web the Content </ title> 
 . 6      <Meta HTTP-equiv =" the Content-the Type "Content =" text / HTML; charset = UTF-. 8 "/>
 . 7 < / head>
 . 8  
. 9  
10 <body>
 . 11      <P TH: text = " 'the Hello,' + $ {name} + '!'" />
 12 is      <- TH: prefix thymeleaf template, corresponding to jstl tag! ->
 13 is </ body>
 14 </ HTML>

run

1. Install: Right-click the project name, Run As [], [] Maven install

2. ed justice exhausted, and then run: Right-click the project name, Run As [], [] Spring Boot App

   

Guess you like

Origin www.cnblogs.com/hzyhx/p/11094393.html