
spring4+MyBatisで検索画面を作っているのですが、うまくいきません。
エラーがでます。
教えてください。
os winodws10、java1.8、tomcat8.0、spring4
java
1package jp.co.kenshu.controller; 2 3import java.util.List; 4 5import org.springframework.beans.BeanUtils; 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.stereotype.Controller; 8import org.springframework.ui.Model; 9import org.springframework.web.bind.annotation.ModelAttribute; 10import org.springframework.web.bind.annotation.PathVariable; 11import org.springframework.web.bind.annotation.RequestMapping; 12import org.springframework.web.bind.annotation.RequestMethod; 13 14import jp.co.kenshu.dto.test.TestDto; 15import jp.co.kenshu.form.SearchForm; 16import jp.co.kenshu.form.TestForm; 17import jp.co.kenshu.service.TestService; 18 19@Controller 20public class TestController { 21 22 @Autowired 23 private TestService testService; 24 25 26 @RequestMapping(value = "/test/{id}", method = RequestMethod.GET) 27 public String test(Model model, @PathVariable int id) { 28 TestDto test = testService.getTest(id); 29 model.addAttribute("message", "MyBatisのサンプルです"); 30 model.addAttribute("test", test); 31 return "test"; 32 } 33 34 @RequestMapping(value = "/test/", method = RequestMethod.GET) 35 public String testAll(Model model) { 36 List<TestDto> tests = testService.getTestAll(); 37 model.addAttribute("message", "MyBatisの全件取得サンプルです"); 38 model.addAttribute("tests", tests); 39 return "test"; 40 } 41 @RequestMapping(value = "/test/insert/input/", method = RequestMethod.GET) 42 public String testInsert(Model model) { 43 TestForm form = new TestForm(); 44 model.addAttribute("testForm", form); 45 model.addAttribute("message", "MyBatisのinsertサンプルです。"); 46 return "testInsert"; 47 } 48 49 @RequestMapping(value = "/test/insert/input/", method = RequestMethod.POST) 50 public String testInsert(@ModelAttribute TestForm form, Model model) { 51 int count= testService.insertTest(form.getName()); 52 return "redirect:/test/"; 53 } 54 @RequestMapping(value = "/test/delete/input/", method = RequestMethod.GET) 55 public String testDelete(Model model) { 56 TestForm form = new TestForm(); 57 model.addAttribute("testForm", form); 58 model.addAttribute("message", "MyBatisのdeleteサンプルです。"); 59 return "testDelete"; 60 } 61 62 @RequestMapping(value = "/test/delete/input/", method = RequestMethod.POST) 63 public String testDelete(@ModelAttribute TestForm form, Model model) { 64 int count = testService.deleteTest(form.getId()); 65 return "redirect:/test/"; 66 } 67 @RequestMapping(value = "/test/update/input/{id}/", method = RequestMethod.GET) 68 public String testUpdate(Model model, @PathVariable int id) { 69 TestDto test = testService.getTest(id); 70 model.addAttribute("message", "MyBatisのUpdateサンプルです"); 71 model.addAttribute("test", test); 72 TestForm form = new TestForm(); 73 form.setId(test.getId()); 74 form.setName(test.getName()); 75 model.addAttribute("testForm", form); 76 return "testUpdate"; 77 } 78 79 @RequestMapping(value = "/test/update/input/{id}/", method = RequestMethod.POST) 80 public String testUpdate(Model model, @ModelAttribute TestForm form) { 81 TestDto dto = new TestDto(); 82 BeanUtils.copyProperties(form, dto); 83 int count = testService.updateTest(dto); 84 return "redirect:/test/"; 85 } 86 87 @RequestMapping(value = "/test/search/", method = RequestMethod.GET) 88 public String TestSearch(Model model) { 89 SearchForm form = new SearchForm(); 90 model.addAttribute("SearchForm", form); 91 model.addAttribute("message", "MyBatisサンプルです。"); 92 return "TestSearch1"; 93 } 94 95 @RequestMapping(value = "/test/search/{name}", method = RequestMethod.GET) 96 public String TestSearch1(@ModelAttribute SearchForm form, Model model) { 97 TestDto test= testService.SearchTest1(form.getName()); 98 model.addAttribute("test",test); 99 return "redirect:/test/search"; 100 } 101 102 103} 104
java
1package jp.co.kenshu.service; 2 3import java.util.LinkedList; 4import java.util.List; 5 6import org.springframework.beans.BeanUtils; 7import org.springframework.beans.factory.annotation.Autowired; 8import org.springframework.stereotype.Service; 9 10import jp.co.kenshu.dto.test.TestDto; 11import jp.co.kenshu.entity.Test; 12import jp.co.kenshu.mapper.TestMapper; 13 14@Service 15public class TestService { 16 17 @Autowired 18 private TestMapper testMapper; 19 20 public TestDto getTest(Integer id) { 21 TestDto dto = new TestDto(); 22 Test entity = testMapper.getTest(id); 23 BeanUtils.copyProperties(entity, dto); 24 return dto; 25 } 26 public List<TestDto> getTestAll() { 27 List<Test> testList = testMapper.getTestAll(); 28 List<TestDto> resultList = convertToDto(testList); 29 return resultList; 30 } 31 32 private List<TestDto> convertToDto(List<Test> testList) { 33 List<TestDto> resultList = new LinkedList<>(); 34 for (Test entity : testList) { 35 TestDto dto = new TestDto(); 36 BeanUtils.copyProperties(entity, dto); 37 resultList.add(dto); 38 } 39 return resultList; 40 } 41 public int insertTest(String name) { 42 int count = testMapper.insertTest(name); 43 return count; 44 } 45 public int deleteTest(int id) { 46 int count = testMapper.deleteTest(id); 47 return count; 48 } 49 50 public int updateTest(TestDto dto) { 51 int count = testMapper.updateTest(dto); 52 return count; 53 } 54 55 public TestDto SearchTest1(String name) { 56 TestDto dto = new TestDto(); 57 Test entity = testMapper.SearchTest1(name); 58 BeanUtils.copyProperties(entity, dto); 59 return dto; 60 } 61 62}
java
1TestSearch1.jsp 2 3<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%> 5<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 6<html> 7<head> 8<meta charset="utf-8"> 9<title>Welcome</title> 10</head> 11<body> 12 <form:form action="${pageContext.request.contextPath}/test/serach/" 13 method="get" modelAttribute="SearchForm"> 14 <form:label path="name">名前:</form:label><form:input path="name" size="40" /><br> 15 <input type="submit" value="Search"/> 16 </form:form> 17 <hr/> 18 <table> 19 <tr> 20 <th>Id</th> 21 <th>Name</th> 22 </tr> 23 <tr> 24 <td>${test.id}</td> 25 <td>${test.name}</td> 26 </tr> 27 </table> 28</body> 29</html> 30
mvc-config.xml <context:component-scan base-package="jp.co.kenshu" /> <mvc:annotation-driven /> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:ValidatorMessages" /> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' --> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> <!-- DataSource --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/test" /> <property name="username" value="" /> <property name="password" value="" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="jp.co.kenshu.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean>
HTTPステータス 400 - 説明 The request sent by the client was syntactically incorrect.
いろいろ考えたり、アドバイスをもらっていろいろ今回直しました。
まだエラーがでて動きません。動くようにしたいです。わかる方教えてください。
この部分のせてとかあれば載せます。
後どなたかこのソースコードでうごくか確認してもらえないでしょうか?
リンク内容
を参考にしました
controller、DTO、Entity、Mapperあっていますでしょうか?
後name検索をspring4とMyBatis3でやっているサイトとかあれば教えてください。
参考にします。
よろしくお願いします。
後jspはうごくようになりました。
名前を入力すると400エラーになります。
回答3件
あなたの回答
tips
プレビュー