您的位置:首页 > 汽车 > 时评 > Web开发:基础Web开发的支持

Web开发:基础Web开发的支持

2024/12/23 23:32:49 来源:https://blog.csdn.net/2301_78714573/article/details/142219911  浏览:    关键词:Web开发:基础Web开发的支持

创建项目:

 添加依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.18</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.ktjiaoyu</groupId><artifactId>SpringBootWeb</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>SpringBootWeb</name><description>SpringBootWeb</description><properties><java.version>1.8</java.version></properties><dependencies><!-- SpringBoot热部署支持(开发阶段使用)--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><!--添加jst1标签库依赖--><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId></dependency><!--添加jsp引擎依赖,Spring Boot内置tomcat没有这个依赖--><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.1.1</version></dependency><!--Web开发依赖--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.18</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.1.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

 Model准备:

package com.ktjiaoyu.crm.service;
import com.ktjiaoyu.crm.pojo.User;
import java.util.List;
public interface UserService {public User login(String usrName, String usrPassword);public int addUser(User user);public int deleteUser(Long usrId);public int updateUser(User user);public User getUser(Long usrId);public List<User> findAllUsers();
}

 创建控制器:

   @RequestMapping(value = "/getUser",method = RequestMethod.GET)public User getUser(Long usrId){User user = userService.getUser(usrId);return user;}

 Web测试的支持:

package com.ktjiaoyu.crm;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;
import javax.annotation.Resource;
@SpringBootTest
@RunWith(SpringRunner.class)
public class UserControllerTest {@Resourceprivate WebApplicationContext wac; // 注入 web 环境private MockMvc mockMvc; // 注入 MockMvc@Beforepublic void setup() {this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); // 初始化 MockMvc}

 测试方法:

package com.ktjiaoyu.crm;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;
import javax.annotation.Resource;
@SpringBootTest
@RunWith(SpringRunner.class)
public class UserControllerTest {@Resourceprivate WebApplicationContext wac; // 注入 web 环境private MockMvc mockMvc; // 注入 MockMvc@Beforepublic void setup() {this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); // 初始化 MockMvc}//根据Id查询@Testpublic void testGetUser() throws Exception {String mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/user/2")) // 测试地址.andReturn().getResponse().getContentAsString(); // 返回响应主体内容System.out.println("Result==" + mvcResult);}//新增(post请求)@Testpublic void testAddUser() throws Exception {//构造请求参数final MultiValueMap<String , String> params = new LinkedMultiValueMap<>();params.add("usrName","SB");params.add("usrPassword","123456");params.add("usrRoleId","2");params.add("usrFlag","1");mockMvc.perform(MockMvcRequestBuilders.post("/user").params(params)).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print());//打印响应结果}
}

 热部署配置:在修改代码后,无需手动重启整个应用程序即可看到修改后的效果。例如,在 Java Web 开发中,对一个 Servlet 类进行了方法的修改,如果开启了热部署,无需重新启动 Tomcat 服务器,节省了从停止到重新启动服务器的等待时间,让开发者可以更快速地进行代码调试和验证。

 

 

总结: 

 大量减少了传统 Spring 项目中繁琐的 XML 配置文件。例如,在 application.properties 或 application.yml 文件中,仅需简单几行配置就能完成如数据库连接、端口号修改等常见设置,如server.port = 8081可轻松更改应用的启动端口。

通过 Spring Initializr 可以轻松创建基于 Spring Boot 的 Web 项目,可根据需求选择 Web、Thymeleaf、JPA 等相关依赖,极大地简化了项目搭建的初始步骤。

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com