老师,您好,麻烦您看一下我的代码,哪里有问题?
项目路径:(jsp都是引用老师的,css也是)
1,DAO类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package com.imooc.dao; import com.imooc.entity.Teacher; import org.springframework.stereotype.Repository; import java.util.Collection; import java.util.HashMap; import java.util.Map; @Repository ( "teacherDao" ) public class TeacherDAO { private Map<Integer, Teacher> map = new HashMap<>(); public void add(Teacher teacher) { map.put(teacher.getId(), teacher); } public void edit(Teacher teacher) { map.put(teacher.getId(), teacher); } public void delete(Integer id) { map.remove(id); } public Teacher selectByID(Integer id) { return map.get(id); } public Collection<Teacher> selectAll() { return map.values(); } } |
2,entity类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package com.imooc.entity; public class Teacher { private Integer id; //教师编号、姓名、性别、主讲课程以及所属学院五个属性。 private String name; private String sex; private String course; private String school; @Override public String toString() { return "Teacher{" + "id=" + id + ", name='" + name + '\ '' + ", sex='" + sex + '\ '' + ", course='" + course + '\ '' + ", school='" + school + '\ '' + '}' ; } public Integer getId() { return id; } public void setId(Integer id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getSex() { return sex; } public void setSex(String sex) { this .sex = sex; } public String getCourse() { return course; } public void setCourse(String course) { this .course = course; } public String getSchool() { return school; } public void setSchool(String school) { this .school = school; } } |
3,handler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | package com.imooc.handler; import com.imooc.dao.TeacherDAO; import com.imooc.entity.Teacher; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import java.util.Collection; @Controller public class RestfulHandler { @Autowired private TeacherDAO teacherDao; @RequestMapping ( "/add" ) public String add(Teacher teacher) { teacherDao.add(teacher); return "redirect:/getAll" ; } @GetMapping ( "/getById/{id}" ) public ModelAndView getById( @PathVariable (value = "id" ) Integer id) { ModelAndView modelAndView = new ModelAndView(); Teacher teacher = teacherDao.selectByID(id); modelAndView.setViewName( "edit" ); modelAndView.addObject( "teacher" , teacher); return modelAndView; } @GetMapping (value = "/getAll" ) public ModelAndView getAll() { ModelAndView modelAndView = new ModelAndView(); Collection<Teacher> teachers = teacherDao.selectAll(); modelAndView.setViewName( "index" ); modelAndView.addObject( "teachers" , teachers); return modelAndView; } @PutMapping (value = "/update" ) public String update(Teacher teacher) { teacherDao.edit(teacher); return "redirect:/getAll" ; } @DeleteMapping (value = "/delete/{id}" ) public String delete( @PathVariable (value = "id" ) Integer id) { teacherDao.delete(id); return "redirect:/getAll" ; } } |
4, spring.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context" xmlns:mvc = "http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> < context:component-scan base-package = "com.imooc" /> <!--配置视图解析器--> < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > < property name = "prefix" value = "/" /> < property name = "suffix" value = ".jsp" /> </ bean > </ beans > |
6, web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > < web-app > < filter > < filter-name >encodingFilter</ filter-name > < filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class > < init-param > < param-name >encoding</ param-name > < param-value >UTF-8</ param-value > </ init-param > < init-param > < param-name >forceEncoding</ param-name > < param-value >true</ param-value > </ init-param > </ filter > < filter-mapping > < filter-name >encodingFilter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > < servlet > < servlet-name >SpringMVC</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet </ servlet-class > <!--配置springmvc.xml的路径--> < init-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:spring.xml</ param-value > </ init-param > </ servlet > < servlet-mapping > < servlet-name >SpringMVC</ servlet-name > < url-pattern >/</ url-pattern > </ servlet-mapping > < filter > < filter-name >hiddenHttpMethodFilter</ filter-name > < filter-class >org.springframework.web.filter.HiddenHttpMethodFilter</ filter-class > </ filter > < filter-mapping > < filter-name >hiddenHttpMethodFilter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > < display-name >Archetype Created Web Application</ display-name > </ web-app > |
正在回答
同学是解决了么?
同学贴的代码中就是RequestMapping呀,同学是指哪里修改为RequestMapping了呢?
祝学习愉快!
最终解决办法:
1,<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
2,
<mvc:annotation-driven >
<!-- 消息转换器 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</mvc:message-converters>
</mvc:annotation-driven>
感谢@看到我麻烦叫我去学习 老铁的回复,在此感谢他。
不知道是不是你的idea做了限制,
@RequestMapping(value = "/add", method =RequestMethod.POST) 应该和@RequestMapping(“/add”)是一样的。
因为@RequestMapping( "/add") 和@RequestMapping(value = "/add") 应该是一样的,
如果不写method应该默认是get和post都可以。
要不你试试@RequestMapping(value = "/add") 看看可不可以。
或者你可以下载老师的源码,看看把老师的@PostMapping(value = "/add")改成
@PostMapping(value = "/add")改成@RequestMapping( "/add") 试试可不可以。
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <? xml version = "1.0" encoding = "UTF-8" ?> < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >com.imooc</ groupId > < artifactId >RegisterLogin</ artifactId > < version >1.0-SNAPSHOT</ version > < packaging >war</ packaging > < name >RegisterLogin Maven Webapp</ name > <!-- FIXME change it to the project's website --> < url >http://www.example.com</ url > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < maven.compiler.source >1.7</ maven.compiler.source > < maven.compiler.target >1.7</ maven.compiler.target > </ properties > < dependencies > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.11</ version > < scope >test</ scope > </ dependency > < dependency > < groupId >javax.servlet</ groupId > < artifactId >javax.servlet-api</ artifactId > < version >3.1.0</ version > </ dependency > <!--导入Springmvc需要的jar --> < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-webmvc</ artifactId > < version >4.3.1.RELEASE</ version > </ dependency > < dependency > < groupId >jstl</ groupId > < artifactId >jstl</ artifactId > < version >1.2</ version > </ dependency > </ dependencies > < build > < finalName >RegisterLogin</ finalName > </ build > </ project > |
- 参与学习 人
- 提交作业 205 份
- 解答问题 4317 个
Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧