使用Spring @Controller
, @RequestMapping
和@ModelAttribute
,我想实现在用户被转发到一个新的JSP设置属性的基本形式提交流程。 Spring提供了不同的方式来实现这一点,但我收到的各种错误。
实施例1基于教程: https://www.baeldung.com/spring-mvc-form-tutorial
form.html
<form action="/submitForm" method="POST">
<input type="text"id="field1" name="field1">
<!-- other input fields -->
<button type="submit">Submit</button>
</form>
的success.jsp
<p>Thanks for signing up ${userName}!!</p>
MyController.java
@Controller
public class MyController{
@RequestMapping(
value = "/submitForm",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String post(@ModelAttribute SignupRequest request, ModelMap model){
// At this point, the SignupRequest is populated correctly
model.addAttribute("userName", request.getUserName());
return "success";
}
}
结果
- 使用
return "success"
-结果是HTTP 404未找到 - 使用
return "success.jsp"
,结果是HTTP 405请求方法“POST”不支持 - 使用
return "redirect:/success.jsp"
,客户端将被重定向,但是属性没有设置,和$ {用户名}是可见的。
基于此接受的答案示例2: Spring MVC中POST方法后重定向
MyController.java
@Controller
public class MyController{
@RequestMapping(
value = "/submitForm",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ModelAndView post(@ModelAttribute SignupRequest request){
// At this point, the SignupRequest is populated correctly
ModelAndView mAV = new ModelAndView("redirect:/success.jsp");
mAV.addObject("userName", request.getUserName());
return mAV;
}
}
结果客户端被重定向,但是属性没有设置,和$ {用户名}是可见的。
什么是这样做的正确方法?
谢谢!
修改的其他详情使用SpringBoot嵌入的Tomcat。 位于JSP文件src>main>resources>public
。 原始JSP正担任。 我相信这个项目不会处理JSP,因为它应该。 添加POM DEPS。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.23.1-GA</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>