Home > Blockchain >  I used the POST method, but there's no mapping for GET
I used the POST method, but there's no mapping for GET

Time:12-15

Even if you change it to the POST method, it keeps saying that there is no mapping for GET.

In the @RequestMapping part, I tried /create and / as well. and I tried it in the GET method, but then the following error appears.

org.springframework.dao.DataIntegrityViolationException:
### Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'title' cannot be null

Wherever I look, I don't know the answer.

Directory: 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 >
        <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 code

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;

 }
}

Service code

package sample.spring.yse;

import java.util.Map;

public interface BookService {
    String create(Map<String, Object> map);
}

CodePudding user response:

I see that you are using POST in your form, however your Controller API Endpoint is not the default /

The JSP form has to specify which action it should use on Submit..

<form method="POST" action="/create">  


</form>
  • Related