我正在尝试从表单中保存数据但是正在获取"客户端发送的请求在语法上是不正确的。 HTTP状态400"。代码如下 的的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>JMyHomeAbroad</display-name>
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/fila/*</url-pattern>
</servlet-mapping>
</web-app>
样品servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!--Annotations activation-->
<context:annotation-config/>
<context:component-scan base-package="com.filasoft"/>
<!-- DB Configuration -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" name="dsObj"
p:driverClassName="oracle.jdbc.OracleDriver"
p:url="jdbc:oracle:thin:@localhost:1521:xe"
p:username='jhome'
p:password='homej'
/>
<!-- ASFB Obj -->
<bean name="sfObj" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dsObj"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.filasoft.model.Volunteer</value>
</list>
</property>
</bean>
<!-- HT Obj -->
<bean name="htObj" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sfObj"/>
</bean>
<!-- UI Configurations -->
<!-- View Resolver Obj -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
volunteer.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="lightblue">
<center>
<h1>Fill all the forms</h1>
<form action="savReg" method="post">
<table>
<tr>
<td>VolId:</td>
<td><input type="text" name="volid"></td>
</tr>
<tr>
<td>First Name:</td>
<td><input type="text" name="firstname"></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lastname"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>Repeat Password:</td>
<td><input type="password" name="verifypassword"></td>
</tr>
<tr>
<td>Gender:</td>
<td><input type="radio" name="gender" value="male" checked="Male">Male
<input type="radio" name="gender" value="female" checked="Female">Female</td>
</tr>
<tr>
<td>Nationality:</td>
<td><input type="text" name="nationality"></td>
</tr>
<tr>
<td>Phone no:</td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td>Current city:</td>
<td><input type="text" name="currentcity"></td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address"></td>
</tr>
</table>
<input type="submit" value="Register"> <input type="reset" value="clear">
</form>
</center>
${msg}
</body>
</html>
volunteer.java - 模型类
package com.filasoft.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="VOL_TAB")
public class Volunteer {
@Id
@Column(name="v_id")
private int volid;
@Column(name="f_name")
private String firstname;
@Column(name="l_name")
private String lastname;
@Column(name="email")
private String email;
@Column(name="pwd")
private String password;
@Column(name="v_pwd")
private String verifypassword;
@Column(name="gen")
private String gender;
@Column(name="nationality")
private String nationality;
@Column(name="phone")
private int phone;
@Column(name="c_city")
private String currentcity;
@Column(name="age")
private int age;
@Column(name="address")
private String address;
public Volunteer() {
}
public Volunteer(int volid) {
super();
this.volid = volid;
}
public Volunteer(int volid, String firstname, String lastname, String email, String password, String verifypassword,
String gender, String nationality, int phone, String currentcity, int age, String address) {
super();
this.volid = volid;
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.password = password;
this.verifypassword = verifypassword;
this.gender = gender;
this.nationality = nationality;
this.phone = phone;
this.currentcity = currentcity;
this.age = age;
this.address = address;
}
@Override
public String toString() {
return "Volunteer [volid=" + volid + ", firstname=" + firstname + ", lastname=" + lastname + ", email=" + email
+ ", password=" + password + ", verifypassword=" + verifypassword + ", gender=" + gender
+ ", nationality=" + nationality + ", phone=" + phone + ", currentcity=" + currentcity + ", age=" + age
+ ", address=" + address + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + age;
result = prime * result + ((currentcity == null) ? 0 : currentcity.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((firstname == null) ? 0 : firstname.hashCode());
result = prime * result + ((gender == null) ? 0 : gender.hashCode());
result = prime * result + ((lastname == null) ? 0 : lastname.hashCode());
result = prime * result + ((nationality == null) ? 0 : nationality.hashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
result = prime * result + phone;
result = prime * result + ((verifypassword == null) ? 0 : verifypassword.hashCode());
result = prime * result + volid;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Volunteer other = (Volunteer) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (age != other.age)
return false;
if (currentcity == null) {
if (other.currentcity != null)
return false;
} else if (!currentcity.equals(other.currentcity))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (firstname == null) {
if (other.firstname != null)
return false;
} else if (!firstname.equals(other.firstname))
return false;
if (gender == null) {
if (other.gender != null)
return false;
} else if (!gender.equals(other.gender))
return false;
if (lastname == null) {
if (other.lastname != null)
return false;
} else if (!lastname.equals(other.lastname))
return false;
if (nationality == null) {
if (other.nationality != null)
return false;
} else if (!nationality.equals(other.nationality))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (phone != other.phone)
return false;
if (verifypassword == null) {
if (other.verifypassword != null)
return false;
} else if (!verifypassword.equals(other.verifypassword))
return false;
if (volid != other.volid)
return false;
return true;
}
public int getVolid() {
return volid;
}
public void setVolid(int volid) {
this.volid = volid;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getVerifypassword() {
return verifypassword;
}
public void setVerifypassword(String verifypassword) {
this.verifypassword = verifypassword;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getNationality() {
return nationality;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public String getCurrentcity() {
return currentcity;
}
public void setCurrentcity(String currentcity) {
this.currentcity = currentcity;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
VolunteerController
package com.filasoft.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.filasoft.model.Volunteer;
import com.filasoft.service.IVolunteerService;
@Controller
public class VolunteerController {
@Autowired
private IVolunteerService service;
@RequestMapping(value="/regvol", method=RequestMethod.GET)
public String showVol(){
return "volunteer";
}
@RequestMapping(value="/savReg", method=RequestMethod.POST)
public String saveVol(@ModelAttribute("volunteer")Volunteer vol,ModelMap map){
int id=service.saveVolunteer(vol);
map.addAttribute("msg", "saved with id:"+id);
return "volunteer";
}
}
IVolunteerService
package com.filasoft.service;
import com.filasoft.model.Volunteer;
public interface IVolunteerService {
public int saveVolunteer(Volunteer vol);
}
VolunteerServiceImpl
package com.filasoft.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.filasoft.dao.IVolunteerDao;
import com.filasoft.model.Volunteer;
import com.filasoft.service.IVolunteerService;
@Service
public class VolunteerServiceImpl implements IVolunteerService{
@Autowired
private IVolunteerDao dao;
@Override
public int saveVolunteer(Volunteer vol) {
return dao.saveVolunteer(vol);
}
}
IVolunteerDao
package com.filasoft.dao;
import com.filasoft.model.Volunteer;
public interface IVolunteerDao {
public int saveVolunteer(Volunteer vol);
}
VolunteerDaoImpl
package com.filasoft.dao.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.filasoft.dao.IVolunteerDao;
import com.filasoft.model.Volunteer;
@Repository
public class VolunteerDaoImpl implements IVolunteerDao{
@Autowired
private HibernateTemplate ht;
@Override
public int saveVolunteer(Volunteer vol) {
return (Integer)ht.save(vol);
}
}
答案 0 :(得分:0)
在此方法中,您的要求客户提交volunteer
@RequestMapping(value="/savReg", method=RequestMethod.POST)
public String saveVol(@ModelAttribute("volunteer")Volunteer vol,ModelMap map){
int id=service.saveVolunteer(vol);
map.addAttribute("msg", "saved with id:"+id);
return "volunteer";
}
但是在你的jsp中,我看到你的表单没有volunteer
。 <form action="savReg" method="post">
您应该将modelAttribute = "volunteer"
添加到form
标记,以使您的表单提交对象类作为控制器中的要求。
我们在here