即使你把它改成POST方法,它也一直說沒有GET的映射。在@RequestMapping 部分,我也嘗試了 /create 和 /。我在 GET 方法中嘗試過,但隨后出現以下錯誤。
org.springframework.dao.DataIntegrityViolationException:
更新資料庫時出錯。原因:java.sql.SQLIntegrityConstraintViolationException:列“標題”不能為空
無論我看哪里,我都不知道答案...幫幫我...
目錄 https://i.stack.imgur.com/8wAqx.png
控制器.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;
}
}
創建.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 >
<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>
服務實作代碼
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);
}
如果有你想要的和弦,請告訴我。我需要幫助...
uj5u.com熱心網友回復:
我看到您在表單中使用 POST,但是您的控制器 API 端點不是默認值 /
JSP 表單必須指定它應該在提交時使用哪個操作。
<form method="POST" action="/create">
</form>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/380403.html
