私は2つのWebページを持っています.1つは "user.jsp"と "person.jsp"です。 "Register"ボタンをクリックすると "user.jsp"送信ボタンをクリックすると "HTTP Status 400"と表示されます。HTTPステータス400取得中にエラーが発生しました
PersonController.java
@Controller
public class PersonController {
private PersonService personService;
private UserService userService;
@Autowired(required=true)
@Qualifier(value="personService")
public void setPersonService(PersonService ps){
this.personService = ps;
}
@RequestMapping(value = "/persons", method = RequestMethod.GET)
public String listPersons(Model model) {
model.addAttribute("person", new Person());
model.addAttribute("listPersons", this.personService.listPersons());
return "person";
}
//For add and update person both
@RequestMapping(value= "/person/add", method = RequestMethod.POST)
public String addPerson(@ModelAttribute("person") Person p,@ModelAttribute("user") User u){
String name = p.getName();
System.out.println("name is :"+name);
if(name == null || name == "") {
System.out.println("user is"+name);
return "user";
}
else{
//new person, add it
this.personService.addPerson(p);
}
return "redirect:/persons";
}
@RequestMapping(value = "/user/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute("user") User user) {
// new user, add to DB
this.userService.addUser(user);
return "redirect:/persons";
}
PersonDAOImpl.java
@Repository
public class PersonDAOImpl implements PersonDAO {
private static final Logger logger = LoggerFactory.getLogger(PersonDAOImpl.class);
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf){
this.sessionFactory = sf;
}
@Override
public void addPerson(Person p) {
Session session = this.sessionFactory.getCurrentSession();
session.persist(p);
logger.info("Person saved successfully, Person Details="+p);
}
@Override
public void updatePerson(Person p) {
Session session = this.sessionFactory.getCurrentSession();
session.update(p);
logger.info("Person updated successfully, Person Details="+p);
}
@SuppressWarnings("unchecked")
@Override
public List<Person> listPersons() {
Session session = this.sessionFactory.getCurrentSession();
List<Person> personsList = session.createQuery("from Person").list();
for(Person p : personsList){
logger.info("Person List::"+p);
}
return personsList;
}
@Override
public Person getPersonByName(String name) {
Session session = this.sessionFactory.getCurrentSession();
List<Person> usersList = session.createQuery(
"from Person ").list();
if (usersList.size() == 0)
return null;
Person usrObj1 = usersList.get(0);
return usrObj1;
}
UserDAOImpl.java
@Repository
public class UserDAOImpl implements UserDAO {
private static final Logger logger = LoggerFactory
.getLogger(UserDAOImpl.class);
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf) {
this.sessionFactory = sf;
}
@Override
public void addUser(User user) {
Session session = this.sessionFactory.getCurrentSession();
session.persist(user);
logger.info("User saved successfully, User Details=" + user);
}
@Override
public void updateUser(User user) {
Session session = this.sessionFactory.getCurrentSession();
session.update(user);
logger.info("User updated successfully, User Details=" + user);
}
@SuppressWarnings("unchecked")
@Override
public List<User> listUsers() {
Session session = this.sessionFactory.getCurrentSession();
List<User> usersList = session.createQuery("from User").list();
for (User user : usersList) {
logger.info("User List::" + user);
}
return usersList;
}
@SuppressWarnings("unchecked")
@Override
public User getUserByName(String fullName) {
Session session = this.sessionFactory.getCurrentSession();
List<User> usersList = session.createQuery(
"from User ").list();
if (usersList.size() == 0)
return null;
User usrObj1 = usersList.get(0);
return usrObj1;
}
Person.java
@Entity
@Table(name="PERSON")
public class Person {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
private String country;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString(){
return "id="+id+", name="+name+", country="+country;
}
}
User.java
@Entity
@Table(name = "user")
@XmlRootElement(name = "User")
public class User {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "fullname")
private String fullName;
@Column(name = "password")
private String password;
@Column(name = "email")
private String email;
@Column(name = "phone")
private Integer phone;
@XmlElement(name = "Id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement(name = "FullName")
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
@XmlElement(name = "Password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@XmlElement(name = "EmailId")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@XmlElement(name = "PhoneNo")
public Integer getPhone() {
return phone;
}
public void setPhone(Integer phone) {
this.phone = phone;
}
PersonServiceImpl.java
@Service
public class PersonServiceImpl implements PersonService {
private PersonDAO personDAO;
public void setPersonDAO(PersonDAO personDAO) {
this.personDAO = personDAO;
}
@Override
@Transactional
public void addPerson(Person p) {
this.personDAO.addPerson(p);
}
@Override
@Transactional
public void updatePerson(Person p) {
this.personDAO.updatePerson(p);
}
@Override
@Transactional
public List<Person> listPersons() {
return this.personDAO.listPersons();
}
@Override
@Transactional
public Person getPersonById(int id) {
return this.personDAO.getPersonById(id);
}
@Override
@Transactional
public Person getPersonByName(String name) {
return this.personDAO.getPersonByName(name);
}
@Override
@Transactional
public void removePerson(int id) {
this.personDAO.removePerson(id);
}
UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {
private UserDAO userDAO;
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
@Override
@Transactional
public void addUser(User user) {
this.userDAO.addUser(user);
}
@Override
@Transactional
public void updateUser(User user) {
this.userDAO.updateUser(user);
}
@Override
@Transactional
public List<User> listUsers() {
return this.userDAO.listUsers();
}
@Override
@Transactional
public User getUserById(int id) {
return this.userDAO.getUserById(id);
}
@Override
@Transactional
public void removeUser(int id) {
this.userDAO.removeUser(id);
}
@Override
@Transactional
public User getUserByName(String fullName) {
return this.userDAO.getUserByName(fullName);
}
}
<c:url var="addAction" value="/person/add" ></c:url>
<form:form action="${addAction}" commandName="person">
<table>
<c:if test="${!empty person.name}">
<tr>
<td>
<form:label path="id">
<spring:message text="ID"/>
</form:label>
</td>
<td>
<form:input path="id" readonly="true" size="8" disabled="true" />
<form:hidden path="id" />
</td>
</tr>
</c:if>
<tr>
<td>
<form:label path="name">
<spring:message text="Name"/>
</form:label>
</td>
<td>
<form:input path="name" />
</td>
</tr>
<tr>
<td>
<form:label path="country">
<spring:message text="Country"/>
</form:label>
</td>
<td>
<form:input path="country" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit"
value="<spring:message text="Add Person"/>" />
<input type="submit"
value="<spring:message text="Register"/>" />
</td>
</tr>
</table>
</form:form>
person.jsp user.jsp
<c:url var="addAction" value="/user/add"></c:url>
<form:form action="${addAction}" commandName="user">
<table width="400px" height="150px">
<c:if test="${!empty user.email}">
<tr>
<td><form:label path="id">
<spring:message text="ID" />
</form:label></td>
<td><form:input path="id" readonly="true" size="8"
disabled="true" /> <form:hidden path="id" /></td>
</tr>
</c:if>
<c:if test="${!empty user.email}">
</c:if>
<tr>
<td><form:label path="fullName">
<spring:message text="Full Name" />
</form:label></td>
<td><form:input path="fullName" /></td>
</tr>
<tr>
<td><form:label path="password">
<spring:message text="Password" />
</form:label></td>
<td><form:input type="password" path="password" /></td>
</tr>
<tr>
<td><form:label path="email">
<spring:message text="Email" />
</form:label></td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td><form:label path="phone">
<spring:message text="Phone" />
</form:label></td>
<td><form:input path="phone" /></td>
</tr>
<tr>
<td colspan="2"><c:if test="${!empty user.email}">
<input type="submit"
value="<spring:message text="Update Details"/>" />
</c:if> <c:if test="${empty user.email}">
<input type="submit" value="<spring:message text="Submit"/>" />
</c:if></td>
</tr>
</table>
</form:form>
この内の任意の提案やアドバイス、それはあなたに
add method =あなたのフォームを "POST"してみてください。 –
person.jspで
を削除してみてください。 –@ GurkanYesilyurt私はそれを試しましたが、まだ同じものを表示しています –