JSP <form action =“ POST”>不重定向到Spring MVC控制器

时间:2019-11-07 18:15:36

标签: spring spring-boot spring-mvc jsp

我刚刚开始使用Spring MVC开发SpringBoot Web应用程序。在我的POC中,我试图显示在上一页中选择的值。

登录屏幕运行正常。一旦单击了具有有效凭据的登录按钮,它就会按预期的那样重定向到success.jsp。现在我期望,当我在success.jsp中单击提交按钮时,它将调用控制器方法[action1()/ action2()]并在控制台中打印消息。

控制器类:

@RequestMapping(value = "login", method = RequestMethod.GET)
    public String init(Model model) {
        model.addAttribute("msg", "Please Enter Your Login Details");
        System.out.println("INIT ====");
        return "login.jsp";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String submit(Model model,
            @ModelAttribute("loginBean") LoginBean loginBean) {
        System.out.println("submit22 ====");
        if (loginBean != null && loginBean.getUserName() != null
                & loginBean.getPassword() != null) {
            if (loginBean.getUserName().equals("a")
                    && loginBean.getPassword().equals("a")) {
                model.addAttribute("msg", loginBean.getUserName());
                return "success.jsp";
            } else {
                model.addAttribute("error", "Invalid Details");
                return "login.jsp";
            }
        } else {
            model.addAttribute("error", "Please enter Details");
            return "login.jsp";
        }
    }

    @RequestMapping(value="/addDetails",params="submit",method=RequestMethod.GET)
    public void action1()
    {
        System.out.println("submit block called GET ");
    }

    @RequestMapping(value="/addDetails",params="submit",method=RequestMethod.POST)
    public void action2()
    {
        System.out.println("submit block called POST");
    }

login.jsp

<form:form name="submitForm" method="POST">

    <div align="center">
        <table>
            <tr>
                <td>User Name</td>
                <td><input type="text" name="userName" /></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" name="password" /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>
        <div style="color: red">${error}</div>

    </div>
</form:form>

success.jsp

<form:form action="/addDetails" method="post">
     <table>
      <tr>
      <td><form:label path = "name">Name : </form:label></td>
        <td>
             <input type="text" name="name">
        </td>
      </tr>
      <tr>
           <td><form:label path = "gender">Gender : </form:label></td>
           <td>
              <input type="radio" name="gender" value="male" > Male 
              <input type="radio" name="gender" value="female"> Female
              <input type="radio" name="gender" value="other"> Other
           </td>
        </tr>         
      <tr>
      <td>
              <input type = "submit" value = "Submit" name ="submit"/>
       </td>
       </tr>       
     </table>
</form:form>

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SampleWebApp</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/application-config.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/mvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

mvc-config.xml:

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation=" http://www.springframework.org/schema/beans    
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
http://www.springframework.org/schema/context   
http://www.springframework.org/schema/context/spring-context-3.0.xsd    
http://www.springframework.org/schema/mvc   
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <mvc:annotation-driven/>
    <context:component-scan base-package="org.dm.springboot"></context:component-scan>
<bean
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
            <property name="prefix" value="/WEB-INF/view/"/>
            <property name="suffix" value=".jsp"/>
    </bean>
</beans>

0 个答案:

没有答案