<mvc:annotation-driven /> 
时间: 2023-04-27 10:06:43 浏览: 31
<mvc:annotation-driven />是Spring MVC框架中的一个配置标签,用于启用注解驱动的控制器和处理器映射器。它会自动注册一些默认的处理器,如RequestMappingHandlerMapping和RequestMappingHandlerAdapter,以便支持使用注解来处理请求和响应。同时,它还会自动注册一些默认的消息转换器,如StringHttpMessageConverter和MappingJackson2HttpMessageConverter,以便支持请求和响应的数据格式转换。使用<mvc:annotation-driven />标签可以简化Spring MVC的配置,提高开发效率。
相关问题
<mvc:annotation-driven/>
<mvc:annotation-driven/> 是 Spring Framework 中用于启用基于注解的 Spring MVC 的配置标签。该标签用于启用注解驱动的处理器映射、适配器和视图解析器。这个标签允许你在Spring MVC应用程序中使用注解来简化配置。
<?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.xsd"> <mvc:annotation-driven/> <context:component-scan base-package="org.example"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> </beans>examplecontext-spring.xml需要写什么
这段XML代码是一个Spring MVC配置文件,它用于配置Spring MVC框架的一些基本设置。具体来说,它完成了以下任务:
1. 启用MVC注解:通过<mvc:annotation-driven/>标签启用Spring MVC注解,这样就可以在Controller中使用注解来处理请求。
2. 扫描组件:通过<context:component-scan>标签指定要扫描的包,将所有标注有@Controller、@Service、@Repository等注解的类自动注册为Bean。
3. 配置视图解析器:通过<bean>标签配置一个InternalResourceViewResolver,将JSP视图解析为InternalResourceView类型的Bean。
因此,如果你的项目中需要使用Spring MVC框架,并且需要使用注解来处理请求,同时还需要配置JSP视图解析器,那么可以根据具体的包结构和JSP路径来修改<context:component-scan>和<bean>标签中的属性值,以适应你的项目需求。
相关推荐








好的,让我来解释一下具体的操作流程:
1. 在index.jsp文件中添加相应请求链接,比如:
添加学生信息
查看书籍列表
2. 在实体类Student中增加一个数组属性private float[] scores;,用于存放所选课程的成绩。
public class Student {
private int id;
private String name;
private float[] scores;
// 省略getter和setter方法
}
3. 修改add_student.jsp,添加成绩输入框,并在表单中加入scores属性。
<form action="addStudent" method="post">
姓名:<input type="text" name="name">
成绩1:<input type="text" name="scores[0]">
成绩2:<input type="text" name="scores[1]">
成绩3:<input type="text" name="scores[2]">
<input type="submit" value="提交"> </form> 4. 修改StudentController.java,将scores属性也加入到Student对象中,并在addStudent方法中获取scores数组。 @Controller public class StudentController { @RequestMapping("/addStudent") public ModelAndView addStudent(Student student) { // 保存学生信息到数据库 // ... // 跳转到学生信息页面 ModelAndView mav = new ModelAndView("student_info"); mav.addObject("student", student); return mav; } } 5. 修改student_info.jsp,将scores数组的值也显示出来。 姓名:<%= student.getName() %>
成绩1:<%= student.getScores()[0] %>
成绩2:<%= student.getScores()[1] %>
成绩3:<%= student.getScores()[2] %>
6. 在pom.xml文件中添加JSON依赖,比如: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version> </dependency> 7. 在webapp目录中创建js文件夹,将jquery-1.11.3.min.js文件复制到js中。 8. 修改spring-mvc.xml文件,加入mvc相关内容,比如: <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> </bean> <mvc:annotation-driven /> 9. 在WEB-INF/pages/book目录中,创建book_list.jsp文件,用于显示和添加book信息如书名,出版社,价格。要求<title>为"测试Json"。 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>测试Json</title> <script src="../js/jquery-1.11.3.min.js"></script> <script> $(document).ready(function() { // 获取所有书籍信息 $.getJSON("bookList", function(data) { // 遍历书籍列表,将每本书的信息添加到表格中 $.each(data, function(index, book) { var tr = $(" ");
tr.append("" + book.name + " ");
tr.append("" + book.publisher + " ");
tr.append("" + book.price + " ");
$("#book_table tbody").append(tr);
});
});
// 添加书籍信息
$("#add_book_form").submit(function(event) {
event.preventDefault(); // 阻止表单提交
// 获取表单数据
var name = $("#name").val();
var publisher = $("#publisher").val();
var price = $("#price").val();
// 发送POST请求,将书籍信息保存到数据库
$.post("addBook", {name: name, publisher: publisher, price: price}, function(data) {
if (data.success) {
// 清空表单,并重新获取书籍列表
$("#name").val("");
$("#publisher").val("");
$("#price").val("");
$("#book_table tbody").empty();
$.getJSON("bookList", function(data) {
$.each(data, function(index, book) {
var tr = $(" ");
tr.append("" + book.name + " ");
tr.append("" + book.publisher + " ");
tr.append("" + book.price + " ");
$("#book_table tbody").append(tr);
});
});
} else {
alert("添加书籍失败!");
}
});
});
});
</script>
</head>
<body>
图书列表
书名
出版社
价格
添加书籍 <form id="add_book_form"> 书名:<input type="text" id="name">
出版社:<input type="text" id="publisher">
价格:<input type="text" id="price">
<input type="submit" value="添加"> </form> </body> </html> 10. 在index.jsp中添加请求链接。 添加学生信息 查看书籍列表 这样就完成了所有的操作。希望能对你有所帮助。
成绩1:<input type="text" name="scores[0]">
成绩2:<input type="text" name="scores[1]">
成绩3:<input type="text" name="scores[2]">
<input type="submit" value="提交"> </form> 4. 修改StudentController.java,将scores属性也加入到Student对象中,并在addStudent方法中获取scores数组。 @Controller public class StudentController { @RequestMapping("/addStudent") public ModelAndView addStudent(Student student) { // 保存学生信息到数据库 // ... // 跳转到学生信息页面 ModelAndView mav = new ModelAndView("student_info"); mav.addObject("student", student); return mav; } } 5. 修改student_info.jsp,将scores数组的值也显示出来。 姓名:<%= student.getName() %>
成绩1:<%= student.getScores()[0] %>
成绩2:<%= student.getScores()[1] %>
成绩3:<%= student.getScores()[2] %>
6. 在pom.xml文件中添加JSON依赖,比如: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version> </dependency> 7. 在webapp目录中创建js文件夹,将jquery-1.11.3.min.js文件复制到js中。 8. 修改spring-mvc.xml文件,加入mvc相关内容,比如: <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> </bean> <mvc:annotation-driven /> 9. 在WEB-INF/pages/book目录中,创建book_list.jsp文件,用于显示和添加book信息如书名,出版社,价格。要求<title>为"测试Json"。 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>测试Json</title> <script src="../js/jquery-1.11.3.min.js"></script> <script> $(document).ready(function() { // 获取所有书籍信息 $.getJSON("bookList", function(data) { // 遍历书籍列表,将每本书的信息添加到表格中 $.each(data, function(index, book) { var tr = $("
添加书籍 <form id="add_book_form"> 书名:<input type="text" id="name">
出版社:<input type="text" id="publisher">
价格:<input type="text" id="price">
<input type="submit" value="添加"> </form> </body> </html> 10. 在index.jsp中添加请求链接。 添加学生信息 查看书籍列表 这样就完成了所有的操作。希望能对你有所帮助。







