下面是我的代码,对于查询方法总是得到null,我不知道为什么会这样。我可以通过其他方式获取表单参数,例如@RequestParam等。我是否需要在XML文件中设置Phone Class的配置?如果是这样,我如何在XML中配置Phone Class?我的XML是recharge-servlet.xml。
以下是控制器:
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";
}
}
形式:
<form action="query" method="POST" name="phone_form">
<section>
<label>phone:</label>
<input type="text" name="phone_number" />
</section>
<input type="submit" value="submit">
</form>
电话类:
package com.nu.template.recharge;
public class Phone {
private String phone_number;
public String getNumber() {
return this.phone_number;
}
public void setNumber(String phone_number) {
this.phone_number = phone_number;
}
}
充值-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>
答案 0 :(得分:0)
您可以通过
初始化Phone对象@RequestMapping(value = "query", method = RequestMethod.POST)
public String query(@RequestParam("phone_number") Phone phone) {
System.out.println(phone.getNumber());
return "helloworld";
}
Spring通常会为绑定查找相同的参数类型,或者您可以告诉它与您的pojo绑定,给定一个接受参数类型的setter方法。