Spring MVC学习初篇
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();
}
}
}
Spring MVC学习初篇的更多相关文章
- Spring MVC 学习第一篇
很好的MVC 参考blog:http://jinnianshilongnian.iteye.com/blog/1752171 MVC: 概念:是一种设计模式,并没有引入新的技术,只是把我们开发的结构组 ...
- Spring MVC 学习总结(十)——Spring+Spring MVC+MyBatis框架集成(IntelliJ IDEA SSM集成)
与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC负责请求的转发和 ...
- Spring MVC学习总结
Spring MVC学习总结 Spring MVC学习路(一) 下载配置文件 Spring MVC学习路(二) 设置配置文件 Spring MVC学习路(三) 编写第一个demo Spring MVC ...
- Spring MVC 学习 -- 创建过程
Spring MVC 学习 -- 创建过程 Spring MVC我们使用的时候会在web.xml中配置 <servlet> <servlet-name>SpringMVC< ...
- 《Spring MVC学习指南》怎么样?答:书名具有很大的欺骗性
2016年6月21日 最近,因为工作需要,我从网上买了一本<Spring MVC学习指南>,ISBN编号: 978-7-115-38639-7,定价:49.00元.此书是[美]Paul D ...
- Spring MVC 学习笔记一 HelloWorld
Spring MVC 学习笔记一 HelloWorld Spring MVC 的使用可以按照以下步骤进行(使用Eclipse): 加入JAR包 在web.xml中配置DispatcherServlet ...
- Spring MVC 学习总结(九)——Spring MVC实现RESTful与JSON(Spring MVC为前端提供服务)
很多时候前端都需要调用后台服务实现交互功能,常见的数据交换格式多是JSON或XML,这里主要讲解Spring MVC为前端提供JSON格式的数据并实现与前台交互.RESTful则是一种软件架构风格.设 ...
- Spring MVC 学习总结(一)——MVC概要与环境配置 转载自【张果】博客
Spring MVC 学习总结(一)--MVC概要与环境配置 目录 一.MVC概要 二.Spring MVC介绍 三.第一个Spring MVC 项目:Hello World 3.1.通过Mave ...
- Spring MVC 学习笔记12 —— SpringMVC+Hibernate开发(1)依赖包搭建
Spring MVC 学习笔记12 -- SpringMVC+Hibernate开发(1)依赖包搭建 用Hibernate帮助建立SpringMVC与数据库之间的联系,通过配置DAO层,Service ...
随机推荐
- 立体匹配:关于用OpenCV彩色化middlebury网站给定的视差
#include "XYZ.h" void readPFM(Mat_<float> &disp, float &scale, string path) ...
- ThinkPHP5.0完全开发手册
http://www.kancloud.cn/manual/thinkphp5/118006 www WEB部署目录(或者子目录) ├─composer.json composer定 ...
- (整理)IIS 7 503 "service unavailable" errors
原文地址:http://mvolo.com/where-did-my-iis7-server-go-troubleshooting-503-quotservice-unavailablequot-er ...
- 37. Binary Tree Zigzag Level Order Traversal && Binary Tree Inorder Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- c/c++运算顺序问题
发现没弄清楚这个问题的人很多,连我们c++老师都没弄清楚,转载一篇文章,以及C++ Primer原文. 裘宗燕:C/C++ 语言中的表达式求值 经常可以在一些讨论组里看到下面的提问:“谁知道下面C语句 ...
- 10. windows与linux文件共享
1. 关闭防火墙 /etc/init.d/iptables stop 2. C:\Users\cfm>ping 192.168.232.131 正在 Ping 192.168.232.131 具 ...
- google快捷键,通过浏览器本身来查看
今天玩google浏览器时发现一个不需要访问google就能查看其快捷键的方式,再此记录一下,以备后用. 1:打开Google浏览器 2:打开开发者工具,有三种方法 2-1:按F12快捷键 2-2:按 ...
- VS调试技巧
下面有从浅入深的6个问题,您可以尝试回答一下 一个如下的语句for (int i = 0; i < 10; i++){if (i == 5)j = 5;},什么都写在一行,你怎么在j=5前面插入 ...
- 主机+虚拟机ubuntu+mini2440开发板互相ping通
折腾这么久,终于将主机,虚拟机和开发板三者之间能够相互ping通,虽然还没有实现我要的功能,不管怎么说先将步骤简单的概括下,用交叉网线将开发板与主机相连,开发板与主机的ip要设置在同一网段内,在配置u ...
- 读取ini配置文件
http://blog.sina.com.cn/s/blog_4d11e5f20100fm2s.html c程序有两种方式传入参数到执行文件中:1.运行exe时,直接输入参数:ping.exe 10. ...