Spring MVC 在下一页传递空值

Spring MVC passes null value on next page

我是 SpringMVC 的初学者 JSP,我正在创建一个使用 POST 方法传递值的简单项目。

第 1 页:hello.jsp

<body>
<h1>Record Form</h1>
    <form name="test" id="test" action="test.jsp" method="post">
        <p>Name: <input type = "text" name = "name" /></p>
        <p>Address: <input type = "text" name = "address" /></p>
        <p>Remarks: <input type = "text" name = "remarks" /></p>
        <p><input type="submit" value="Save" /> <input type="reset" value="Reset" /></p>
    </form>
</body>

第 2 页:test.jsp

<body>
<h1>Result</h1>
    <p>name: ${record.name}</p>
    <p>address: ${record.address}</p>
    <p>remarks: ${record.remarks}</p>
    <a href="hello.jsp">Submit another message</a>
</body>

Record.java

import org.springframework.stereotype.Component;

@Component
public class Record {

    private String name;
    private String address;
    private String remarks;
 //setters getters..

HelloController.java

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

import org.springframework.beans.factory.annotation.Autowired;


@Controller
public class HelloController {

    @Autowired


    @RequestMapping(value = "/")
    public String hello(Record record) {
        return "hello";
    }

    @RequestMapping(value = "/test", method = POST)
    public String test(@RequestParam("name") String name, @RequestParam("address") String address, @RequestParam("remarks") String remarks, Model model) {
        Record record = new Record();
        record.setName(name);
        record.setAddress(address);
        record.setRemarks(remarks);
        model.addAttribute("record", record);
        return "/test";
    }
}

我的问题是,当我点击提交时,没有传递任何值。我一直在检查我的代码,但看不出有什么问题。谁能帮帮我吗?

仅将代码更改为 <form action="test"> 时,会出现此错误。另外,如果我根据搜索的内容更改为 <form action="/test">。什么都不管用。见下图。

我已经在 web.xml

<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></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>

对于我的 pom.xml,我已经添加了这个

 <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>
    </dependencies>

试试这些东西 MAC

像这样更改您的 web.xml:

<welcome-file-list> 
 <welcome-file>hello.jsp</welcome-file> 
</welcome-file-list> 
<servlet> 
<servlet-name>mvc</servlet-name> 
 <servlet-class> 
  org.springframework.web.servlet.DispatcherServlet 
 </servlet-class> 
 <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
 <servlet-name>mvc</servlet-name> 
 <url-pattern>/</url-pattern> 
</servlet-mapping> 
<context-param> 
 <param-name>contextConfigLocation</param-name> 
 <param-value> 
  /WEB-INF/mvc-servlet.xml 
 </param-value> 
</context-param>

确保 mvc-servlet.xml/WEB-INF/ 位置

此外,在 mvc-servlet.xml 中指定 jsp 文件的位置,如

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

(@MAC - 在你的情况下你所有的 jsp 文件都在根级别。)

这可能会帮助您解决问题。

基本上,你需要改变这个:

<form name="test" id="test" action="test.jsp" method="post">

为此:

<form name="test" id="test" action="test" method="post">

否则你映射到/test的controller方法将不会被调用,直接渲染test.jsprecord模型变量为空