我正在开发JAVA Restful Web应用程序。早先所有功能都能正常工作。但是现在我遇到一个错误,说
'HTTP状态400-错误的请求 类型状态报告
说明由于某些东西被认为是客户端错误(例如,格式错误的请求语法,无效的请求消息框架或欺骗性的请求路由),服务器无法或不会处理请求。
Apache Tomcat / 8.5.31'
我无法弄清楚我的代码有什么问题...寻求帮助 谢谢!
///////这是我的模块类-(Student.java)
package com.joseph.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Student {
@Id
@Column
@GeneratedValue(strategy=GenerationType.AUTO) //for autonumber
private int studentId;
@Column
private String firstname;
@Column
private String lastname;
@Column
private int yearLevel;
public Student(){}
public Student(int studentId, String firstname, String lastname,
int yearLevel) {
super();
this.studentId = studentId;
this.firstname = firstname;
this.lastname = lastname;
this.yearLevel = yearLevel;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
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 int getYearLevel() {
return yearLevel;
}
public void setYearLevel(int yearLevel) {
this.yearLevel = yearLevel;
}
}
/////// DAO类(StudentDao.java)
package com.joseph.dao;
import java.util.List;
import com.joseph.model.Student;
public interface StudentDao {
public void add(Student student);
public void edit(Student student);
public void delete(int studentId);
public Student getStudent(int studentId);
public List getAllStudent();
public List searchStudent(String srch);
}
///// DAO实现类(StudentDaoImpl.java)
package com.joseph.dao.impl;
import java.util.List;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.joseph.dao.StudentDao;
import com.joseph.model.Student;
@Repository
public class StudentDaoImpl implements StudentDao {
JdbcTemplate template;
@Autowired
private SessionFactory session;
@Autowired
public void setDatsSource(DataSource dataSource) {
template = new JdbcTemplate(dataSource);
}
@Override
public void add(Student student) {
//session.getCurrentSession().save(student);
String sql = "insert into student(firstname, lastname, yearLevel) values ('"+student.getFirstname()+"', '"+student.getLastname()+"', '"+student.getYearLevel()+"')";
template.update(sql);
}
@Override
public void edit(Student student) {
String sql = "update student set firstname = '"+student.getFirstname()+"', lastname = '"+student.getLastname()+"', yearLevel = '"+student.getYearLevel()+"' where studentId = '"+student.getStudentId()+"'";
template.update(sql);
//session.getCurrentSession().update(student);
}
@Override
public void delete(int studentId) {
String sql = "delete from student where studentId = '"+studentId+"'";
String sql2 = "select count(*) from student";
template.update(sql);
int cnt = template.queryForObject(sql2, Integer.class);
System.out.println(cnt);
}
@Override
public Student getStudent(int studentId) {
return (Student)session.getCurrentSession().get(Student.class, studentId);
}
@Override
public List getAllStudent() {
String sql = "select * from student";
return template.queryForList(sql);
}
@Override
public List searchStudent(String stdID) {
String srch = "%" + stdID + "%";
String sql = "select * from student where studentId like '"+srch+"' OR firstname like '"+srch+"'";
return template.queryForList(sql);
}
}
/////学生服务(StudentService.java)
package com.joseph.service;
import java.util.List;
import com.joseph.model.Student;
public interface StudentService {
public void add(Student student);
public void edit(Student student);
public void delete(int studentId);
public Student getStudent(int studentId);
public List getAllStudent();
public List searchStudent(String srch);
}
/////学生服务实现(StudentServiceImpl.java) 包com.joseph.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.joseph.dao.StudentDao;
import com.joseph.model.Student;
import com.joseph.service.StudentService;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Transactional
public void add(Student student) {
studentDao.add(student);
}
@Transactional
public void edit(Student student) {
studentDao.edit(student);
}
@Transactional
public void delete(int studentId) {
studentDao.delete(studentId);
}
@Transactional
public Student getStudent(int studentId) {
return studentDao.getStudent(studentId);
}
@Transactional
public List getAllStudent() {
return studentDao.getAllStudent();
}
@Transactional
public List searchStudent(String srch) {
return studentDao.searchStudent(srch);
}
}
//控制器类/////
package com.joseph.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.joseph.model.Student;
import com.joseph.service.StudentService;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/index")
public String setupForm(Map<String, Object> map){
Student student = new Student();
map.put("student", student);
map.put("studentList", studentService.getAllStudent());
return "student";
}
@RequestMapping(value="/student.do", method=RequestMethod.POST)
public String doActions(@ModelAttribute Student student, BindingResult result, @RequestParam String action, @RequestParam String searchVal, Map<String, Object> map){
Student studentResult = new Student();
String opr = action.toLowerCase();
if(opr.equals("add")) {
studentService.add(student);
studentResult = student;
map.put("student", studentResult);
map.put("studentList", studentService.getAllStudent());
return "student";
}
else if(opr.equals("edit")) {
studentService.edit(student);
studentResult = student;
map.put("student", studentResult);
map.put("studentList", studentService.getAllStudent());
return "student";
}
else if(opr.equals("delete")) {
studentService.delete(student.getStudentId());
studentResult = new Student();
map.put("student", studentResult);
map.put("studentList", studentService.getAllStudent());
return "student";
}
else if(opr.equals("load")) {
Student searchedStudent = studentService.getStudent(student.getStudentId());
studentResult = searchedStudent!=null ? searchedStudent : new Student();
map.put("student", studentResult);
return "studentEdit";
}
else if(opr.equals("search")) {
System.out.println(searchVal);
map.put("student", studentResult);
map.put("studentList", studentService.searchStudent(searchVal));
return "student";
}
else {
map.put("student", studentResult);
map.put("studentList", studentService.getAllStudent());
return "student";
}
}
}
// JSP表单-Student.jsp ///
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ include file="/WEB-INF/jsp/includes.jsp"%>
<!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>Student Management</title>
</head>
<body>
<h1>Students Data</h1>
<form:form action="student.do" method="POST" commandName="student">
<table>
<tr>
<td><input type="text" name="searchVal" /></td>
<td><input type="submit" name="action" value="Search" /></td>
</tr>
<tr>
<td>First name</td>
<td><form:input type="text" path="firstname" /></td>
</tr>
<tr>
<td>Last name</td>
<td><form:input type="text" path="lastname" /></td>
</tr>
<tr>
<td>Year Level</td>
<td><form:input type="text" path="yearLevel" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="action" value="Add" />
</td>
</tr>
</table>
</form:form>
<br>
<table border="1">
<th>ID</th>
<th>First name</th>
<th>Last name</th>
<th>Year level</th>
<c:forEach items="${studentList}" var="student">
<tr>
<td>${student.studentId}</td>
<td>${student.firstname}</td>
<td>${student.lastname}</td>
<td>${student.yearLevel}</td>
<form:form action="student.do" method="POST" commandName="student">
<form:input path="studentId" value="${student.studentId}" hidden="hidden"/>
<td><input type="submit" name="action" value="Load" /></td>
<td><input type="submit" name="action" value="Delete" /></td>
</form:form>
</tr>
</c:forEach>
</table>
</body>
</html>
答案 0 :(得分:0)
@Controller用于将类标记为Spring MVC Controller,它将响应一个视图
@RestController用于Restful API。这是一个方便的批注,其中包括@Controller和@ResponseBody批注
$data = [
'name' => $contract->name. '_copy',
// other fields ...
];
$entity = $this->Contracts->newEntity($data);
$this->Contracts->save($entity);
// ...
// $this->redirect($this->referer());
更改为
@Controller // please try to update this line.
public class StudentController {
...
}