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