Forward to JSP within Spring Controller after form submission

人盡茶涼 提交于 2020-01-25 10:05:46

问题


Using Spring @Controller, @RequestMapping and @ModelAttribute, I'd like to achieve a basic form submission flow in which the user is forwarded to a new JSP with attributes set. Spring provides different ways to achieve this, but I have received various errors.

Example 1 Based on tutorial: 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";
    }
}

Results

  1. Using return "success" - the result is HTTP 404 Not Found
  2. Using return "success.jsp", the result is HTTP 405 Request method 'POST' not supported
  3. Using return "redirect:/success.jsp", the client is redirected, but attributes are not set, and ${userName} is visible.

Example 2 Based on the accepted answer here: Redirect after POST method in spring MVC

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;
    }
}

Result the client is redirected, but attributes are not set, and ${userName} is visible.

What is the correct way to do this?

Thanks!

EDIT Additional details Using SpringBoot with embedded Tomcat. JSP file located in src>main>resources>public. The raw JSP is being served. I believe the project is not treating JSP as it should. Adding 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>

回答1:


@Controller
public class MyController{

    @RequestMapping(
            value = "/submitForm", 
            method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public RedirectView post(@ModelAttribute SignupRequest request, RedirectAttributes ra){
        // At this point, the SignupRequest is populated correctly 

        RedirectView rw = new RedirectView();
        rw.setUrl("success.jsp");
        ra.addFlashAttribute("userName", request.getUserName());

        return rw;
    }
}


来源:https://stackoverflow.com/questions/58053918/forward-to-jsp-within-spring-controller-after-form-submission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!