gpt4 book ai didi

java - 在 spring mvc 中提交表单时出现 HTTP 状态 400 错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:47:52 25 4
gpt4 key购买 nike

我有两个网页。一个是“user.jsp”和“person.jsp”。当我单击“注册”按钮时,它会转到“user.jsp”,当我单击提交按钮时,它会显示“Http Status 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;
}
}

用户.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);
}

}

person.jsp

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

用户.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>

任何建议或意见都会对我有帮助谢谢你

最佳答案

希望这会有所帮助!

https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

400 Bad Request The server cannot or will not process the request due to an apparent client error (e.g., malformed request syntax, too large size, invalid request message framing, or deceptive request routing).

关于java - 在 spring mvc 中提交表单时出现 HTTP 状态 400 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42154588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com