Article directory
- Task 1. Set the home page of the project - index.jsp
- Task 2. Add a login link on the home page, click to jump to the login page
- Task 3. Use the Spring MVC configuration file to realize quick page jump
- Task 4. Add static resources and let Spring MVC handle them correctly
- Task 5. Request a simple string returned by the server
- Task 6. Request the JSON data returned by the server
- Task 7. Request the XML data returned by the server
Task 1. Set the home page of the project - index.jsp
Page display content - Welcome to Spring MVC World! and the current date and time of the system
1. Modify the web.xml file
Comment out the "set startup homepage" element (deletion is also ok)
2. Create a home page file
Create a home page file in the views directory - index.jsp
Description: <%@ page import="java.util.Date" %>
- Page instructions
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首页</title>
</head>
<body>
<h1>Welcome to Spring MVC World~</h1>
<h3><%= new Date() %></h3> <!--JSP表达式元素-->
</body>
</html>
At this point, start the server to see the effect
3. Modify the login controller
In the login controller, add a method in LoginController to jump to the home page
@RequestMapping("/")
public String index() {
return "index";
}
4. Start the server and check the effect
Visit: http://localhost:8080/SpringMvcDemo/
Refresh the page, the time will change
Task 2. Add a login link on the home page, click to jump to the login page
1. Modify the home page file
Add a hyperlink to jump to the login page and
restart the server
Click the [Jump to login page] hyperlink
to modify the hyperlink element
2. Modify the login controller
Add a method toLogin() to jump to the login page in LoginController
3. Start the server and check the effect
Display the home page
and click the [Jump to login page] hyperlink
Description: After the project is started, jumping to the home page, and then jumping from the home page to the login page are all realized through the jump method in the login controller. There is no business logic in the jump method, just a return statement is responsible for the page jump Turn, we have a simpler method to replace it, that is, to use the Spring MVC configuration file to achieve quick page jump.
Task 3. Use the Spring MVC configuration file to realize quick page jump
1. Modify the Spring MVC configuration file
Define two view controllers to be responsible for page jumps
<!--定义视图控制器-->
<mvc:view-controller path="/" view-name="index"/>
<mvc:view-controller path="/toLogin" view-name="login"/>
2. Modify the login controller
Delete two methods responsible for page jumps: index(), toLogin()
3. Start the server and check the effect
Display the home page (indicating that the first view controller in the Spring MVC configuration file takes effect)
and click the [Jump to login page] hyperlink to jump to the login page (indicating that the second view controller in the Spring MVC configuration file is also took effect)
Task 4. Add static resources and let Spring MVC handle them correctly
Static resources include pictures, style sheets, scripts, audio and video, and the following uses pictures as an example to illustrate
1. Add a picture
Create an images directory in WEB-INF and copy the picture bear.jpg in (of course you can copy other pictures)
2. Modify the home page file
Add elements to display pictures
3. Start the server and check the effect
The home page cannot display images normally, which means that the application cannot access static image resources
4. Modify the Spring MVC configuration file
Handle static resources separately
5. Modify the home page file
The image source uses a virtual path, and the configuration file is responsible for mapping to the real path
6. Restart the server and check the effect
The home page shows a picture of a bear
classroom exercises
(1) Add CSS style sheet
Create a css directory in WEB-INF, and create a style file index.css in it, which is responsible for the style of the home page (all elements are centered, the background color of the page is set, and the underline of the hyperlink is removed...)
(2) Add JavaScript script
Create a js directory in WEB-INF, and create a script file check.js in it, which is responsible for the non-empty verification of the login page (first, the user name is not empty, and then the password is not empty)
Task 5. Request a simple string returned by the server
1. Create a string controller
Get String Controller - GetStringController
package net.army.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 作者:梁辰兴
* 日期:2023/5/11
* 功能:获取字符串控制器
*/
@Controller
public class GetStringController {
@GetMapping("/getString")
@ResponseBody // 响应正文注解,表明返回普通字符串而不是返回逻辑视图名
public String getString() {
return "Spring MVC 有趣~";
}
}
2. Start the server and check the effect
Visit: http://localhost:8080/SpringMvcDemo/getString
3. Modify the acquisition string controller
Sets the encoding used for the returned string
4. Restart the server and check the effect
Visit: http://localhost:8080/SpringMvcDemo/getString
Thinking question: Can the returned string be styled?
Set to return normal text, the return string does not recognize webpage tags
Set to return the web page text, return the string to recognize the web page label
Task 6. Request the JSON data returned by the server
1. Create a user entity class
Create the net.army.spring.bean package, and then create the User class in the package.
The user entity class corresponds to the user table
package net.army.spring.bean;
import java.util.Date;
/**
* 作者:梁辰兴
* 日期:2023/5/11
* 功能:用户实体类
*/
public class User {
private int id;
private String username;
private String password;
private String telephone;
private Date registerTime;
private int popedom;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Date getRegisterTime() {
return registerTime;
}
public void setRegisterTime(Date registerTime) {
this.registerTime = registerTime;
}
public int getPopedom() {
return popedom;
}
public void setPopedom(int popedom) {
this.popedom = popedom;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", telephone='" + telephone + '\'' +
", registerTime=" + registerTime +
", popedom=" + popedom +
'}';
}
}
2. Create and get JSON controller
Get JSON controller - GetJsonController
Here, there is no operation of the database to get the user entity or user list, and how to deal with it when integrating the SSM framework later.
package net.army.spring.controller;
import net.army.spring.bean.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 作者:梁辰兴
* 日期:2023/5/11
* 功能:
*/
@RestController // @RestController是@Controller和@ResponseBody的结合
public class GetJsonController {
@GetMapping(value = "/getJson", produces = "application/json; charset=utf-8")
public User getJson() {
// 创建用户对象
User user = new User();
// 设置用户对象属性
user.setId(1);
user.setUsername("张舰艇");
user.setPassword("123456");
user.setTelephone("13890903456");
user.setRegisterTime(new Date());
user.setPopedom(1);
// 返回用户对象(按照请求映射注解的参数设置转换成JSON)
return user;
}
@GetMapping(value = "/getJsonArray", produces = "application/json; charset=utf-8")
public List<User> getJsonArray() {
// 创建用户列表
List<User> users = new ArrayList<>();
// 创建第1个用户
User user = new User();
user.setId(1);
user.setUsername("张舰艇");
user.setPassword("123456");
user.setTelephone("13890903456");
user.setRegisterTime(new Date());
user.setPopedom(1);
// 将用户添加到用户列表
users.add(user);
// 创建第2个用户
user = new User();
user.setId(2);
user.setUsername("江山而");
user.setPassword("222222");
user.setTelephone("13856567890");
user.setRegisterTime(new Date());
user.setPopedom(1);
// 将用户添加到用户列表
users.add(user);
// 创建第3个用户
user = new User();
user.setId(3);
user.setUsername("桑清所");
user.setPassword("333333");
user.setTelephone("15890905678");
user.setRegisterTime(new Date());
user.setPopedom(1);
// 将用户添加到用户列表
users.add(user);
// 返回用户列表
return users;
}
}
3. Add JSON dependencies to the project
Add support for json in the pom.xml file
<!--对json的支持-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
4. Start the server and view the results
Visit: http://localhost:8080/SpringMvcDemo/getJson
The newly added dependencies need to be placed in the output directory
Restart the server and visit again: http://localhost:8080/SpringMvcDemo/getJson to view the effect
Visit: http://localhost:8080/SpringMvcDemo/getJsonArray
Task 7. Request the XML data returned by the server
1. Create and get XML controller
Get the XML controller - GetXmlController
package net.army.spring.controller;
import net.army.spring.bean.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 作者:梁辰兴
* 日期:2023/5/11
* 功能:获取XML控制器
*/
@RestController
public class GetXmlController {
@GetMapping(value = "/getXml", produces = "application/xml; charset=utf-8")
public List<User> getXml() {
// 创建用户列表
List<User> users = new ArrayList<>();
// 创建第1个用户
User user = new User();
user.setId(1);
user.setUsername("张舰艇");
user.setPassword("123456");
user.setTelephone("13890903456");
user.setRegisterTime(new Date());
user.setPopedom(1);
// 将用户添加到用户列表
users.add(user);
// 创建第2个用户
user = new User();
user.setId(2);
user.setUsername("江山而");
user.setPassword("222222");
user.setTelephone("13856567890");
user.setRegisterTime(new Date());
user.setPopedom(1);
// 将用户添加到用户列表
users.add(user);
// 创建第3个用户
user = new User();
user.setId(3);
user.setUsername("桑清所");
user.setPassword("333333");
user.setTelephone("15890905678");
user.setRegisterTime(new Date());
user.setPopedom(1);
// 将用户添加到用户列表
users.add(user);
// 返回用户列表
return users;
}
}
2. Add XML dependencies to the project
Add support for xml in the pom.xml file
<!--对xml的支持-->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.5.3</version>
</dependency>
3. Add dependencies to the output directory
After adding dependencies to the output directory in the project structure window
4. Start the server and check the effect
Visit: http://localhost:8080/SpringMvcDemo/getXml
unsolved!