Spring 控制器无法将参数传递给 jsp

Spring controller cannot passing parameter to jsp

我是 spring 的新人。我正在尝试将参数从控制器发送到 jsp 文件。我已经按照spring教程进行操作,但是我发送给jsp的参数值仍然为空。我不知道我的代码有什么问题。

这是我的代码, web.xml

<display-name>OPTIMA Dashboard</display-name>

<servlet>
    <servlet-name>app</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <multipart-config>
        <max-request-size>418018841</max-request-size>
        <file-size-threshold>1048576</file-size-threshold>
    </multipart-config>
</servlet>

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

应用-servlet.xml

<context:property-placeholder location="classpath:${mule.env}.properties" />
<mvc:annotation-driven />

<mvc:resources mapping="/production/**" location="/production/" />
<mvc:resources mapping="/admin/build/**" location="/build/" />
<mvc:resources mapping="/admin/css/**" location="/css/" />
<mvc:resources mapping="/admin/vendors/**" location="/vendors/" />
<mvc:resources mapping="/build/**" location="/build/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/vendors/**" location="/vendors/" />
<mvc:resources mapping="/favicon.ico" location="/favicon.ico" />

<context:component-scan base-package="com.jpa.optima.admin.controller" />

<bean id="multipartResolver"
    class="org.springframework.web.multipart.support.StandardServletMultipartResolver" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/production/" />
    <property name="suffix" value=".jsp" />
</bean>

LoginController.java

@Controller
@RequestMapping(value = "/admin")
public class LoginController {

@Autowired
private SessionProcessor sessionProcessor;
@Autowired
private HazelcastInstance instance;
@Autowired
private ContextLoader contextLoader;

@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(Model model) {
    ModelAndView modelAndView = new ModelAndView("login");
    modelAndView.addObject("key", contextLoader.getSiteKey());
    modelAndView.addObject("login", new Login());
    return modelAndView;
}

login.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>OPTIMA Dashboard</title>

<!-- Bootstrap -->
<link href="vendors/bootstrap/dist/css/bootstrap.min.css"
rel="stylesheet">
<!-- Font Awesome -->
<link href="vendors/font-awesome/css/font-awesome.min.css"
rel="stylesheet">
<!-- NProgress -->
<link href="vendors/nprogress/nprogress.css" rel="stylesheet">
<!-- Animate.css -->
<link href="vendors/animate.css/animate.min.css" rel="stylesheet">

<!-- Custom Theme Style -->
<link href="build/css/custom.min.css" rel="stylesheet">
<!-- Google Recaptcha v2 -->
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>

<body class="login">
<div>
    <a class="hiddenanchor" id="signup"></a> <a class="hiddenanchor"
        id="signin"></a>

    <div class="login_wrapper">
        <div class="animate form login_form">
            <section class="login_content">
                <form method="POST" action="/admin/submitLogin" commandName="index"
                    modelAttribute="login">
                    <h1>Login Form</h1>

                    <div>
                        <span
                            style="border: none; box-shadow: none; font-size: 12; color: red;">${status}</span>
                        <input type="text" class="form-control" placeholder="Username"
                            name="username" required="" />
                    </div>
                    <div>
                        <input type="password" class="form-control"
                            placeholder="Password" name="password" required="" />
                    </div>
                    <div class="g-recaptcha" data-sitekey="${key}"></div>
                    <br />
                    <div>
                        <button type="reset" class="btn btn-default submit">Reset</button>
                        <button type="submit" class="btn btn-default submit">Log
                            In</button>
                    </div>
                    <div class="clearfix"></div>

                    <div class="separator">
                        <div class="clearfix"></div>
                        <br />

                        <div>
                            <h1>
                                <i class="fa fa-paw"></i> OPTIMA
                            </h1>
                            <p>
                                <i class="fa fa-copyright"></i> 2017 Jatelindo Perkasa Abadi
                            </p>
                        </div>
                    </div>
                </form>
            </section>
        </div>
    </div>
</div>
</body>
</html>

任何人都可以帮助我找出我的代码有什么问题。

谢谢

在 LoginController.java 中,您应该使用 POST 方法,因为您从 jsp.

发送数据为 POST
@RequestMapping(value = "/login", method = RequestMethod.POST)

我已经找到问题了。我只需要更改我的代码:

@RequestMapping(value = "/login", method = RequestMethod.GET)

成为 @RequestMapping(value = "/admin/login", method = RequestMethod.GET)