设定 pom.xml
首先,我们在建立项目时,一定要先设定POM,导入spring boot使用相关Web的Jar文件引入进来。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nandin</groupId>
<artifactId>first-web</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<finalName>myweb</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build></project>
设定 application.yml
server:
port : 8081
http2:
enabled: true
servlet:
session:
timeout: 24h
context-path: /
spring:
application :
name : myweb
main:
lazy-initialization: true
mvc:
view:
prefix : /WEB-INF/jsp/
suffix : .jsp
启用主程式
Spring Boot的设定大多都是采用annotation设计,并且都是以@当做tag,因此在主程式中,一定要在class的name上方加入**@SpringBootApplication**。
package com.nandin.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyFirstWebApplication {
public static void main(String[] args) {
SpringApplication.run(MyFirstWebApplication.class, args);
}
}
Web Controller
当我们在页面链接时,需要设定入口以代表操作页面,例如http://xxx.com/user/list/conact,设定方式就会如下。若有设定application.yml的context-path的话,就必须加入其名称,例如:/myweb,那么链接则需加入,例如:http://xxx.com/myweb/user/list/conact
package com.nandin.web.controller;import java.util.ArrayList;import java.util.List;import org.springframework.http.MediaType;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import com.nandin.bean.Contact;
@Controller
@RequestMapping(path = "/user", produces = MediaType.APPLICATION_JSON_VALUE, consumes = {
MediaType.APPLICATION_JSON_VALUE })public class AppController {
@RequestMapping(value = "/list/contact", method = RequestMethod.GET) public String listContact(Model model) {
List<Contact> listContact = new ArrayList<>();
listContact.add(new Contact("Tom Smith", "tomsmith@outlook.com"));
listContact.add(new Contact("John Purcell", "john123@yahoo.com"));
listContact.add(new Contact("Siva Krishna", "sivakrishna@gmail.com"));
model.addAttribute("contacts", listContact); return "pages/user/contact";
}
}
Contact Model
package com.nandin.web.bean;
import java.io.Serializable;
public class Contact implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1583181781842875526L;
private String name;
private String email;
public Contact() {
super();
}
public Contact(String name, String email) {
super();
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
页面显示
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>我的第一个网页</title></head><body>
<h1 align="center">联系人清单</h1>
<br />
<table border="1" cellpadding="10">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr th:each="contact : ${contacts}">
<td th:text="${contact.name}">Name</td>
<td th:text="${contact.email}">Email</td>
</tr>
</tbody>
</table></body></html>
启动服务
启动我们主程式MyFirstWebApplication.java
0 评论