spring-mvc 相关问题

基于模型 - 视图 - 控制器(MVC)模式构建Java Web应用程序的框架。它从底层视图技术中提升灵活和分离的代码。

为什么Spring中找不到JSP?文件存在

在一个非常基本的应用程序中,我有 sayHelloController.java 如下 @控制器 公共类 SayHelloController { @RequestMapping(“说你好-jsp”) 公共字符串 sayHelloJsp() { ...

回答 3 投票 0

从 Spring MVC @RestController 返回视图

由于@RestController是@Controller和@ResponseBody的组合,我相信如果我希望我的控制器同时工作 MVC 和 REST 控制器只需使用 @RestController 进行注释就可以了。是...

回答 3 投票 0

使用 MockMvc 测试发送带有文件的对象

我想将一个对象发送到控制器,该对象具有多个包含文件的列表和多个包含纯文本的字段。 公共类贡献新 { 私人名单 我想将一个对象发送到控制器,该对象具有多个包含文件的列表和多个包含纯文本的字段。 public class ContributionNew<T extends MovieInfoDTO> { private List<T> elementsToAdd; private Map<Long, T> elementsToUpdate; private Set<Long> idsToDelete; private Set<String> sources; private String comment; } public class Photo extends MovieInfoDTO { private MultipartFile photo; } @PostMapping(value = "/{id}/contributions/photos") @ResponseStatus(HttpStatus.CREATED) public ResponseEntity<Void> createPhotoContribution( @ApiParam(value = "The movie ID", required = true) @PathVariable("id") final Long id, @ApiParam(value = "The contribution", required = true) @RequestBody @Valid final ContributionNew<Photo> contribution ) { 我想创建一个测试来发送对象,但我不知道如何完成它。 @Test public void testCreatePhotoContribution() throws Exception { ContributionNew<Photo> contribution = new ContributionNew<>(); MockMultipartFile multipartFile = new MockMultipartFile("photo", "C:\\Users\\Jonatan\\Pictures\\2.png", "image/png", "Spring Framework".getBytes()); Photo.Builder photoBuilder = new Photo.Builder( multipartFile ); contribution.getElementsToAdd().add(photoBuilder.build()); mockMvc .perform(post("/api/v1.0/movies/{id}/contributions/photos", 1) .contentType(...) .content(...)) .andExpect(status().isCreated()); } 我不知道如何为传输的数据设置正确的类型,设置内容。仅有关仅发送文件(不在对象中)的教程可用。但没有指南表明文件是对象中的字段之一。怎么办? 对于那些想知道的人: 您可以发送 MultipartFile 和一些纯文本字段,以映射到控制器中的 1 个对象。看看我的例子。 达: public class NewFileInfoDto { @NotNull @Positive() private Long requestId; @NotNull private MultipartFile file; } 控制器: @PostMapping @ResponseStatus(HttpStatus.CREATED) public FileInfoDto addFile(@ModelAttribute @Valid NewFileInfoDto dto) { return fileService.addFile(dto); } 测试部分: mockMvc.perform(multipart("/file") .file(file) .param("requestId", "3") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isCreated()); ```

回答 1 投票 0

带有 Tomcat 的 Spring MVC 仅渲染index.jsp

问题是,当我使用 Tomcat 运行我的应用程序时,它在浏览器中仅显示 index.jsp。但控制器不起作用。 这是我的 web.xml 文件: 问题是,当我使用 Tomcat 运行我的应用程序时,它在浏览器中仅显示 index.jsp。但控制器不起作用。 这是我的web.xml文件: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>spring-course-mvc</display-name> <absolute-ordering /> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> applicationContext.xml 文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.myapp.spring.mvc" /> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> </beans> 我的控制器: package com.myapp.spring.mvc; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MyController { @RequestMapping("/") public String showFirstView() { return "first-view"; } } 我的 view 所在的 webapp/WEB-INF/ 中有 first-view.jsp 目录。所以看起来webapp/WEB-INF/view/first-view.jsp 当我在 http://localhost:8080/spring_course_mvc/ 上运行项目时,出现 404 错误。 有趣的是,当我添加 webapp/index.jsp 文件时,我在 http://localhost:8080/spring_course_mvc/ 上看到其内容。 然后我添加了 webapp/first-view.jsp 文件和地址 http://localhost:8080/spring_course_mvc/first-view.jsp 它正在工作。 没有 webapp/index.jsp 地址 http://localhost:8080/spring_course_mvc/ 会给我 404 错误。 首先,您需要确保如果您使用的是Spring 5,则需要使用Tomcat 9。 如果您使用的是 Spring 6,则需要使用 Tomcat 10。 接下来您需要确保您的 web.xml 和 applicationContext.xml 位于 webapp/WEB-INF/web.xml 和 webapp/WEB-INF/applicationContext.xml。 您需要记住的另一件事是,如果您的应用程序不会自动运行浏览器,您需要确保使用正确的上下文根值。在 Eclipse 中,您可以通过右键单击项目 > 属性 > Web 项目设置来检查。

回答 1 投票 0

注释 @Valid 在我的 REST 应用程序中不起作用

导入jakarta.validation.Valid; 导入 org.springframework.beans.factory.annotation.Autowired; 导入 org.springframework.http.HttpStatus; 导入 org.springframework.http.ResponseEntity; 导入组织。

回答 1 投票 0

在 Spring 中的 servlet 过滤器内启用 Hibernate 过滤器

我正在开发一个多租户应用程序,这意味着我需要能够根据拥有请求用户配置文件的组织帐户来过滤 API 响应。在我们的模型中,这是参考...

回答 1 投票 0

在 Thymeleaf 和 spring MVC 中提交表单上发布对象列表

我在后端有一个名为 PatientDto 的类。 @数据 @AllArgsConstructor @NoArgs构造函数 公共类 PatientDto { 私有字符串nationalId; 私有字符串名字; 私人

回答 1 投票 0

获取执行器/运行状况端点的状态代码为“DOWN”

我最近将我的 spring-boot 项目从 2.0 版本更新到了 2.7.5,我收到了一些与文档PluginsBootstrapper 相关的错误,并且 swagger 没有打开。 通过此链接,我添加了 bean 和新的

回答 2 投票 0

在 Java Spring Boot 3.x 中将 PDF 转换为 Excel +

我有一个关于使用 Spring Boot 框架将 pdf 文件转换为 Excel 文件的新任务。 如果有人向我推荐 Maven 依赖项和代码,这对我会有很大帮助。 @PostMapping("/

回答 1 投票 0

在form:options中添加数据属性,即在options.tag中

我在 pageContext 中有一个对象列表,我想要在 select 的选项标签的数据属性中使用它的一些属性。 下面是jsp代码: 我在 pageContext 中有一个对象列表,我想要在 select 的选项标签的数据属性中使用它的一些属性。 下面是jsp代码: <form:select class="required chzn_a" disabled="${view}" path="one.id" id="one"> <form:option value="">Select</form:option> <form:options items="${objectList}" itemValue="id" itemLabel="name"/> </form:select> 我想要如下所示的东西: 但是,以下是我得到的: 据我所知 form:options 标签不支持这样的自定义。相反,您可以像这样自己循环选择选项: <form:select class="required chzn_a" disabled="${view}" path="one.id" id="one"> <form:option value="">Select</form:option> <c:forEach items="${objectList}" var="objectListEntry"> <form:option value="${objectListEntry.id}" data-code="${objectListEntry.code}">${objectListEntry.name}</form:option> </c:forEach> </form:select> Spring 表单标签实现了 DynamicAttributes 接口,并且 tld 中未显式定义的所有属性都将添加到生成的标签中。 我遇到了同样的问题,如何将属性(ISO 3 字母国家代码)添加到选择的选项中。一本字典和一点 jQuery 效果很好: var countryDict = {}; // make a dictionary of {country ID : country iso Code} countryList.forEach(element => { countryDict[element.id] = element.iso3Code; }); $('#myCountry > option').each(function(){ // use that dictionary to add ISO code as an attribute to country options. $(this).attr('isoCode', countryDict[$(this).val()]); }); 使用更改 tld 文件覆盖 spring 选项标签 创建新参数标签并在 tld 中定义 类似这个 public class OptionsTagNew extends OptionsTag { @Getter List<OptionalAttributeOptionModel> optAttr = new ArrayList<>(); @Override protected void writeOptionalAttributes(TagWriter tagWriter) throws JspException { super.writeOptionalAttributes(tagWriter); for (OptionalAttributeOptionModel x : optAttr) { writeOptionalAttribute(tagWriter, x.getName(), x.getValue()); } } public void setOptAttr(OptionalAttributeOptionModel optAttr) { this.optAttr.add(optAttr); } } @Getter @Setter @AllArgsConstructor class OptionalAttributeOptionModel { private String name; private String value; } @Getter @Setter public final class AddOptionAttributeTag extends TagSupport { private String name; private String path; @Override public int doStartTag() throws JspException { OptionsTagNew parent = (OptionsTagNew) findAncestorWithClass(this, OptionsTagNew.class); if (parent == null) { throw new JspException("AddOptionAttributeTag must be within a OptionTag!"); } parent.setOptAttr(new OptionalAttributeOptionModel(name, getPathValue(parent.getItems()))); return SKIP_BODY; } private String getPathValue(Object items) { BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(items); return Objects.requireNonNull(wrapper.getPropertyValue(this.path)).toString(); } @Override public int doEndTag() throws JspException { return super.doEndTag(); } @Override public void release() { this.name = null; this.path = null; super.release(); } }

回答 3 投票 0

Spring Security的授权

当我输入已注册并输入数据库的正确数据时,spring security 写入数据不正确 谁能帮忙,我用的是spring security 6,我刚刚开始学习...

回答 1 投票 0

Spring Security如何将principal注入到Controller中?

我可以像下面的代码一样获取用户主体,但我很困惑Spring Security如何知道注入正确的主体。通常,我们会传递 args 来调用带参数的方法。那么,哪里

回答 2 投票 0

当我在代码中使用 thymeleaf 并将标头代码替换为单独的文件时,然后不显示标头选项卡

我正在学习 Spring Boot 并在我的项目中使用 Thymeleaf。当我将标头代码替换为同一文件夹中的单独文件并使用 Thymeleaf 的替换语法、CSS、JS 和 Bootstrap 代码时...

回答 1 投票 0

尝试将 spring 版本从 4.3.7 RELEASE 升级到 5.3.10 出现错误

以下是我收到的错误消息: [错误] 无法在项目 tspservice-dataService 上执行目标 org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile): Compilat...

回答 0 投票 0

尝试运行Spring boot 3.2.4应用程序时获取org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.class

我正在开发 Spring Boot 3.2.4 应用程序,我已将密文放置在 application.properties 文件中并解密它,我在 pom.xml 文件中添加了下面提到的依赖项,但是当我运行时...

回答 1 投票 0

根据 Spring Boot 中另一个字段的值设置 POJO 字段值

我有一个简单的 POJO 类 POJO 类{ 私有布尔值已启用, 私有字符串字段 } 我有一个接受 POJO 类型的 RequestBody 的控制器,作为更改的一部分,我想包含验证

回答 1 投票 0

@Data 和 Lombok 中的一些 getter/setter 存在一些错误

我不知道为什么,但我的对象类 Product 无法使用 Lombok 创建 getter/setter。这有道理吗? 这是 Product 对象类。 包com.lab2.model; 导入 lombok.NoArgsConstruct...

回答 1 投票 0

Spring Bean ClassNotFound 如何调试?

我是 Spring 新手,收到以下错误 java.lang.NoClassDefFoundError: org/springframework/beans/factory/NoUniqueBeanDefinitionException java.lang.Class.getDeclaredMethods0(本机

回答 2 投票 0

为什么 @JsonInclude(JsonInclude.Include.NON_EMPTY) 不起作用?

我正在尝试将空值设置为 JSON 有效负载中的空值数据库。造成这个问题的原因是我对社会实体领域有独特的限制。 我有一个请求 DTO,如下所示: @Va...

回答 1 投票 0

预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段

我在前端使用 angularjs2 并使用 Java Spring 作为后端 Rest api 并收到错误。 XMLHttpRequest 无法加载“某些 url”。 Access-Control 不允许请求标头字段 appkey...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.