首页 技术 正文
技术 2022年11月14日
0 收藏 329 点赞 4,887 浏览 3991 个字

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();
}
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295