Java Spring mvc无法获取表单参数
下面是我的代码,查询方法总是为null,我不知道为什么会发生这种情况。我可以通过其他方式获取表单param,如@RequestParam等。我是否需要在XML文件中设置Phone类的配置?如果是这样,如何在XML中配置Phone类?我的XML是reserve-servlet.XML 以下是控制器:Java Spring mvc无法获取表单参数,java,spring,Java,Spring,下面是我的代码,查询方法总是为null,我不知道为什么会发生这种情况。我可以通过其他方式获取表单param,如@RequestParam等。我是否需要在XML文件中设置Phone类的配置?如果是这样,如何在XML中配置Phone类?我的XML是reserve-servlet.XML 以下是控制器: package com.nu.template.recharge; import org.springframework.stereotype.Controller; import org.spri
package com.nu.template.recharge;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/mobile")
public class XJMobile {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView getIndexPage() {
ModelAndView mav = new ModelAndView("recharge");
return mav;
}
@RequestMapping(value = "/query", method = RequestMethod.POST)
public String query(Phone phone) {
System.out.println(phone.getNumber());
return "helloworld";
}
}
表格:
reserve-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.nu.template.recharge"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
您可以通过以下方式初始化手机对象:
@RequestMapping(value = "query", method = RequestMethod.POST)
public String query(@RequestParam("phone_number") Phone phone) {
System.out.println(phone.getNumber());
return "helloworld";
}
Spring通常会寻找相同的参数类型进行绑定,或者您可以告诉它绑定到您的pojo,给定一个接受参数类型的setter方法。表单操作不应该是mobile/query吗?@Tony Vu但是如果表单操作的路径有问题,我想我也不能通过@RequestParam获得param,对吗,您的财产,即getter/setter是命名号码,而不是电话号码。与输入名称不匹配。@getter/setter是否必须使用电话号码命名?我使用number是因为当我检查在线资源时,人们会这样写:public void setStudentIDstudentID{this.studentID=studentID;},set后面的名称以大写字母S开头,它与studentID不同,所以我想我可以对setter/getter使用任何名称。我会重新命名,然后再试一次。你的getter/setter很好。问题是setter是setNumber,应该为输入字段调用它,从而调用名为phone_number的参数。这显然不匹配。所以setter应该是setPhone_number,或者更好,输入应该是named number。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.nu.template.recharge"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
@RequestMapping(value = "query", method = RequestMethod.POST)
public String query(@RequestParam("phone_number") Phone phone) {
System.out.println(phone.getNumber());
return "helloworld";
}