Spring mvc 使用配置:

 <!-- 使用MVC -->
<servlet>
<servlet-name>defaultDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- Spring XML 文件 -->
<param-value>classpath:school-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>defaultDispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.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.0.xsd">
<bean name="/test1/helloworld" class="com.yinuo.web.controller.HelloWorldController" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

  访问静态资源(js、css、img等)

<!-- 访问静态资源   -->
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/img/" mapping="/img/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>

Spring  mvc 注解:

<!-- 注解扫描包 -->
<context:component-scan base-package="cn.com.yinuo.school"></context:component-scan> <!-- 开启注解 -->
<mvc:annotation-driven/>
<!-- 上面开启注解的方式是下面开启注解方式的优化,下面的方式在3之后就过时了 -->
<!--
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
-->
@Controller
@RequestMapping("/book")
public class UserController { @RequestMapping(value="/addBook",method=RequestMethod.POST)
public ModelAndView addBook(){
String result ="this is addBook------";
return new ModelAndView("/bookList","result",result);
}
@RequestMapping(value="/delBook",method=RequestMethod.GET)
public ModelAndView delUser(){
String result ="this is delBook------";
return new ModelAndView("/bookList","result",result);
}
   //去掉method=RequestMethod.GET /.POST表示两者皆可
@RequestMapping("/updateBook")
public String updateBook(){
return "/bookList";
}
}

Spring 参数传递:

  单个域传递:

  Controller方法的参数名字与表单域的名字一致,即可直接获得表单的数据

  对象封装传递:

  封装到一个bean里面。bean的属性名与表单域的name属性名称一致,并且bean提供getter和setter方法,在controller即可直接bean.getXxx()获得。

  json传递 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新增书籍</title>
<script type="text/javascript">
$(document).ready(function(){
$("#add").click(function(){
var bookName = $("#bookName").attr("value");
var price =$("#price").attr("value");
var book = {bookName:bookName,price:price};
$.ajax({
url:"/book/addBook",
type:"post",
data:book,
success:function(bk){
console.info("bookName--->" + bk.bookName + "price--->" + bk.price );
}
});
});
});
</script>
</head>
<body>
<h>新增书籍</h>
书名<input type="text" id="bookName" name="bookName">
价格<input type="text" id="price" name="price">
<input type="button" id="add" value="新增">
</body>
</html>
@Controller
@RequestMapping("/book")
public class BookController {
@RequestMapping("/addBook")
public void addUserJson(Book book,HttpServletRequest request,HttpServletResponse response){
String result = "{\"bookName\":\" "+ book.getBookName() +" \",\"price\":\" "+ book.getPrice()+" \"}";
PrintWriter out = null;
response.setContentType("application/json");
try {
out = response.getWriter();
out.write(result);
} catch (IOException e) {
e.printStackTrace();
}
}
}

最新文章

  1. HTML是什么?如何使用?
  2. Server Error in &#39;/&#39; Application
  3. oracle11g 修改字符集
  4. Css 常用属性
  5. 《Unix网络编程》卷2 读书笔记 第3章- System V IPC
  6. STM32 UART 重映射
  7. Python使用UUID库生成唯一ID(转)
  8. WinForm应用程序退出的方法
  9. 【官方文档】《暗黑世界V1.4》API说明!
  10. Oracle11G安装
  11. bzoj 1188 : [HNOI2007]分裂游戏 sg函数
  12. .net的自定义JS控件,运用了 面向对象的思想 封装 了 控件(.net自定义控件开发的第一天)
  13. Swashbuckle.AspNetCore3.0的二次封装与使用
  14. &lt;TCP/IP原理&gt; (一)
  15. sql的with as用法
  16. windows的cmd批处理命令及powershell (二)
  17. js 获取属性名称
  18. websocket 工作原理
  19. Spring Batch框架流程的简单介绍
  20. python2编码的问题

热门文章

  1. C预处理和C库
  2. (LinkedList)Intersection of Two Linked Lists
  3. 基于Node.js+socket.IO创建的Web聊天室
  4. 仿淘宝详情转场(iOS,安卓没有这功能)
  5. oracle中的case when then else end 用法
  6. 洛谷P3374 【模板】树状数组 1
  7. go中方法的接收者是值或者指针的区别
  8. js 分享到按钮
  9. 正则验证:Pattern,Matcher
  10. js 小工具-- 获取主机名