我使用了 POST 方法,但是没有 GET 的映射
I used the POST method, but there's no mapping for GET
即使改成POST方法,也一直说GET没有映射
在@RequestMapping 部分,我也尝试了/create 和/。
然后在GET方法中试了一下,然后出现如下错误
org.springframework.dao.DataIntegrityViolationException:
### Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'title' cannot be null
无论我看哪里,我都不知道答案。
目录:https://i.stack.imgur.com/8wAqx.png
Controller.java
package sample.spring.yse;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class BookController {
@Autowired
BookService bookService;
@RequestMapping(value = "/create", method= RequestMethod.POST)
public ModelAndView createPost(@RequestParam Map<String, Object> map) {
ModelAndView mav = new ModelAndView();
String bookId = this.bookService.create(map);
if (bookId == null) {
mav.setViewName("redirect:/create");
}else {
mav.setViewName("redirect:/detail?bookId=" + bookId);
}
return mav;
}
}
create.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>책 생성하기</title>
</head>
<body>
<h1>create book</h1>
<form method="post">
<p>제목 : <input type="text" name="title"></p>
<p>카테고리 : <input type="text" name="category"></p>
<p>가격 : <input type="text" name="price"></p>
<p><input type="submit" value="save"></p>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
</body>
</html>
servlet-content.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="sample.spring.yse" />
</beans:beans>
book_SQL.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="book">
<insert id="insert" parameterType="hashMap" useGeneratedKeys="true" keyProperty="book_id">
<![CDATA[
insert into book
(title, category, price)
values
(#{title}, #{category}, #{price})
]]>
</insert>
</mapper>
ServiceImpl 代码
package sample.spring.yse;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl implements BookService {
@Autowired
BookDao bookDao;
@Override
public String create(Map<String, Object> map) {
int affectRowCount = this.bookDao.insert(map);
if (affectRowCount == 1) {
return map.get("book_id").toString();
}
return null;
}
}
服务代码
package sample.spring.yse;
import java.util.Map;
public interface BookService {
String create(Map<String, Object> map);
}
我看到您在表单中使用 POST,但是您的控制器 API 端点不是默认值 /
JSP 表单必须指定它应在提交时使用的操作..
<form method="POST" action="/create">
</form>
即使改成POST方法,也一直说GET没有映射
在@RequestMapping 部分,我也尝试了/create 和/。 然后在GET方法中试了一下,然后出现如下错误
org.springframework.dao.DataIntegrityViolationException:
### Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'title' cannot be null
无论我看哪里,我都不知道答案。
目录:https://i.stack.imgur.com/8wAqx.png
Controller.java
package sample.spring.yse;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class BookController {
@Autowired
BookService bookService;
@RequestMapping(value = "/create", method= RequestMethod.POST)
public ModelAndView createPost(@RequestParam Map<String, Object> map) {
ModelAndView mav = new ModelAndView();
String bookId = this.bookService.create(map);
if (bookId == null) {
mav.setViewName("redirect:/create");
}else {
mav.setViewName("redirect:/detail?bookId=" + bookId);
}
return mav;
}
}
create.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>책 생성하기</title>
</head>
<body>
<h1>create book</h1>
<form method="post">
<p>제목 : <input type="text" name="title"></p>
<p>카테고리 : <input type="text" name="category"></p>
<p>가격 : <input type="text" name="price"></p>
<p><input type="submit" value="save"></p>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
</body>
</html>
servlet-content.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="sample.spring.yse" />
</beans:beans>
book_SQL.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="book">
<insert id="insert" parameterType="hashMap" useGeneratedKeys="true" keyProperty="book_id">
<![CDATA[
insert into book
(title, category, price)
values
(#{title}, #{category}, #{price})
]]>
</insert>
</mapper>
ServiceImpl 代码
package sample.spring.yse;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl implements BookService {
@Autowired
BookDao bookDao;
@Override
public String create(Map<String, Object> map) {
int affectRowCount = this.bookDao.insert(map);
if (affectRowCount == 1) {
return map.get("book_id").toString();
}
return null;
}
}
服务代码
package sample.spring.yse;
import java.util.Map;
public interface BookService {
String create(Map<String, Object> map);
}
我看到您在表单中使用 POST,但是您的控制器 API 端点不是默认值 /
JSP 表单必须指定它应在提交时使用的操作..
<form method="POST" action="/create">
</form>