SpringMVC_初级总结

1.SpringMVC的工作原理

  1. 浏览器发出一个http请求给服务器,如果匹配DispatcherServlet的请求映射路径(在web.xml中指定),服务器将请求转交给DispatcherServlet.
  2. DipatcherServlet接收到这个请求之后,根据请求的路径,将请求转交给相应的Controller
  3. Controller处理后,返回ModelAndView对象(封装跳转页面,要传递的数据)
  4. DispatcherServlet根据ViewResolver视图解析器,找到ModelAndView指定的JSP。(ModelAndView只是逻辑视图并不是一个正式的视图)
  5. 将JSP显示到浏览器中

2.代码

1.项目目录

2.ApplicationContext-mvc.xml

<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 如果显示找不到mvc:resources标签而报错的话,可能因为编辑器中的schema过于陈旧,这时将
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd的结尾的3.0改成3.2即可 --><!-- 要扫描的包 --><context:component-scan base-package="org.jsoft.action."/><!-- 启用注解和对对应目录下静态资源的访问 --><mvc:annotation-driven/>	<mvc:resources mapping="/assets/**" location="/assets/"/><!-- 视图解析器 --><bean id="viewResolver"class="org.springframework.web.servlet.view.UrlBasedViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/><property name="prefix" value="/"/><property name="suffix" value=".jsp"/></bean>
</beans>

3.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- 配置springMVC的servlet --><servlet><servlet-name>springMvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 配置ApplicationContext.xml的路径 --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/ApplicationContext-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springMvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
</web-app>

4.vo类

public class Student {private Long id;private String name;private Integer age;/**getter and setter*/
}

5.C层StudentAction

import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.jsoft.vo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;@Controller
@RequestMapping("studentAction")
public class StudentAction {//方法一,返回的是一个字符串,根据视图解析器,会自动在前面加上/,在后面加上".jsp",在访问这个jsp页面@RequestMapping("/login")public String login(){System.out.println("StudentAction.login()");return "index";}//方法二,返回一个最常用的ModelAndView,里面可以添加要传向浏览器的数据和页面@RequestMapping("/register")public ModelAndView register(){Student student = new Student();student.setAge(50);student.setName("PinkFloyd");student.setId(1L);ModelAndView mv = new ModelAndView();mv.setViewName("register");//设置返回的页面mv.addObject("student", student);//添加键值对形式的返回数据mv.addObject("firstName", "David");mv.addObject("lastName", "Gilmour");return mv;}//方法三,需要接受页面数据时可以在方法的参数里添加request和response@RequestMapping("/update")//这是Ajax形式的请求public void update(HttpServletRequest request,HttpServletResponse response) throws Exception{request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");Student student = new Student();String id = request.getParameter("id");String name = request.getParameter("name");String age = request.getParameter("age");student.setAge(new Integer(age));student.setName(name);student.setId(new Long(id));PrintWriter out = response.getWriter();out.print(JSONObject.fromObject(student).toString());//已json的形式返回}
}

6.V层register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>My JSP 'register.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script type="text/javascript" src="assets/js/jquery-1.10.1.js"></script></head><body>Register.jsp <br/>Welcome!${student.id},${student.age},${student.name}乐队的<%=request.getAttribute("firstName") %>  ${lastName}<form id="f">乐队id:<input name="id" value="${student.id}"/>乐队年龄:<input name="age" value="${student.age}"/>乐队名称:<input name="name" value="${student.name}"/><input id="btn" type="button" value="修改"></form><script type="text/javascript" >$(function(){$("#btn").click(function(){$.ajax({url:"studentAction/update",type:"post",data:$("#f").serialize(),dataType:"json",success : function(msg){alert(msg.name);}})})});</script></body>
</html>

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/331113.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

tomcat中配置jndi数据源以便spring获取

【0】README0&#xff09;intro to jndi&#xff0c; plase visit intro to jndi&#xff1b;1&#xff09;本文译自 Configuring Spring MVC JdbcTemplate with JNDI Data Source in Tomcat&#xff1b;2&#xff09;本文旨在分析如何通过springmvc 获取 JNDI 数据源 以连接到…

Machine Learning:十大机器学习算法

转载自 Machine Learning&#xff1a;十大机器学习算法摘要: - 机器学习算法分类&#xff1a;监督学习、无监督学习、强化学习 - 基本的机器学习算法&#xff1a;线性回归、支持向量机(SVM)、最近邻居(KNN)、逻辑回归、决策树、k平均、随机森林、朴素贝叶斯、降维、梯度增强 机…

Java的值传递解析

值传递与引用传递 最近学基础的时候&#xff0c;老师讲了值传递和引用传递&#xff0c;这个问题一直不太明白&#xff0c;上网查了很多资料&#xff0c;按照自己的理解整理了一遍&#xff0c;发现之前不太明白的地方基本上想明白了&#xff0c;如有不正确的地方&#xff0c;欢…

spring(13)缓存数据

【0】README1&#xff09;本文部分文字描述转自&#xff1a;“Spring In Action&#xff08;中/英文版&#xff09;”&#xff0c;旨在review “spring(13)缓存数据” 的相关知识&#xff1b;2&#xff09;缓存&#xff1a;缓存可以存储经常会用到的信息&#xff0c;这样每次需…

漫画:什么是分布式事务

转载自 漫画&#xff1a;什么是分布式事务&#xff1f;————— 第二天 —————————————————假如没有分布式事务 在一系列微服务系统当中&#xff0c;假如不存在分布式事务&#xff0c;会发生什么呢&#xff1f;让我们以互联网中常用的交易业务为例子&#…

Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合

【0】README0&#xff09;本文旨在 review Spring4.2.6SpringMVC4.2.6MyBatis3.4.0 整合过程&#xff1b;1&#xff09;项目整合所涉及的源代码&#xff0c;please visit https://github.com/pacosonTang/MyBatis/tree/master/spring4mvc_mybatis32&#xff09;由于晚辈我还不…

ibatis(0)ibatis 与 mybatis 简述

【0】README:1&#xff09;本文旨在给出 ibatis 与 mybatis 简述&#xff0c;简述内容转自 如下链接&#xff1b;【1】main contents1&#xff09;apache offical declarationhttp://ibatis.apache.org/.apache ibatis is retired at the apache software foundation (2010/06/…

Java面试大纲

转载自 金三银四跳槽季&#xff0c;Java面试大纲跳槽时时刻刻都在发生&#xff0c;但是我建议大家跳槽之前&#xff0c;先想清楚为什么要跳槽。切不可跟风&#xff0c;看到同事一个个都走了&#xff0c;自己也盲目的面试起来&#xff08;期间也没有准备充分&#xff09;&#x…

ibatis(1)ibatis的理念

【0】README1&#xff09;本文部分内容转自 “ibatis in action”&#xff0c;旨在 review “ibatis的理念” 的相关知识&#xff1b;【1】结合所有优秀思想的混合型解决方案在现实世界中&#xff0c;混合型解决方案随处可见。将两个看上去相悖的思想在中间处巧妙结合&#xff…

究竟啥才是互联网架构“高并发”

转载自 究竟啥才是互联网架构“高并发”一、什么是高并发 高并发&#xff08;High Concurrency&#xff09;是互联网分布式系统架构设计中必须考虑的因素之一&#xff0c;它通常是指&#xff0c;通过设计保证系统能够同时并行处理很多请求。高并发相关常用的一些指标有响应时间…

ibatis(2)ibatis是什么

【0】README1&#xff09;本文部分内容转自 “ibatis in action”&#xff0c;旨在 review “ibatis是什么” 的相关知识&#xff1b;2&#xff09;intro to ibatis&#xff1a; ibatis 就是数据映射器&#xff0c;Martin Fowler在《企业应用架构模式》中&#xff0c;对 data m…

究竟啥才是互联网架构“高可用”

转载自 究竟啥才是互联网架构“高可用”一、什么是高可用 高可用HA&#xff08;High Availability&#xff09;是分布式系统架构设计中必须考虑的因素之一&#xff0c;它通常是指&#xff0c;通过设计减少系统不能提供服务的时间。 假设系统一直能够提供服务&#xff0c;我们说…

maven(3)maven3.3.9使用入门

【0】README1&#xff09;maven 安装step1&#xff09;检查 jdk 是否安装且 环境变量 JAVA_HOME 是否设置&#xff1b;step2&#xff09;download maven&#xff1a; https://maven.apache.org/download.cgi?Preferredftp://mirror.reverse.net/pub/apache/step3&#xff09;…

TCP接入层的负载均衡、高可用、扩展性架构

转载自 TCP接入层的负载均衡、高可用、扩展性架构 一、web-server的负载均衡 互联网架构中&#xff0c;web-server接入一般使用nginx来做反向代理&#xff0c;实施负载均衡。整个架构分三层&#xff1a; 上游调用层&#xff0c;一般是browser或者APP 中间反向代理层&#xff…

使用poi统计工作职责

1 创建一个新的sheet工作页 Sheet job workbook.createSheet("工作职责统计"); 2 查询工作职责问题选项列表&#xff0c;并设置第一行倒出时间 List<Syslistconfig> listconfigs syslistconfigDao.listConfig(29); //工作职责问题选项列表job.createRow(0)…

漫画:什么是字典序算法

转载自 漫画&#xff1a;什么是字典序算法&#xff1f;算法题目&#xff1a; 给定一个正整数&#xff0c;实现一个方法来求出离该整数最近的大于自身的“换位数”。 什么是换位数呢&#xff1f;就是把一个整数各个数位的数字进行全排列&#xff0c;从而得到新的整数。例如53241…

mybatis_user_guide(2)mybatis3.4.0快速入门

【0】README0&#xff09;以下部分内容转自&#xff1a;“mybatis v.3.4.0 User Guide”&#xff1b;1&#xff09;本文旨在梳理 如何 构建 mybatis 环境&#xff0c;与 db 连接&#xff0c;且采用 JUnit 搭建其测试用例&#xff1b;2&#xff09;本文的环境配置都是基于纯 my…

jQuery中的几个案例:隔行变色、复选框全选和全不选

1 表格隔行变色 1 技术分析&#xff1a; 1 &#xff09;基本过滤选择器&#xff1a; odd: even: 2 &#xff09;jq添加和移除样式&#xff1a; addClass(); removeClass(); 2 代码实现 <script src"js/jquery1.11.3/jquery.min.js" type"text/javasc…

从 Linux 源码看 Socket 的阻塞和非阻塞

转载自 从 Linux 源码看 Socket 的阻塞和非阻塞笔者一直觉得如果能知道从应用到框架再到操作系统的每一处代码&#xff0c;是一件Exciting的事情。大部分高性能网络框架采用的是非阻塞模式。笔者这次就从linux源码的角度来阐述socket阻塞(block)和非阻塞(non_block)的区别。 本…

pojo和javabean的区别

【0】README 1&#xff09;本文转自&#xff1a; http://wenku.baidu.com/view/eba89bbcf121dd36a32d828a.html 【1】正文如下&#xff1a; POJO 和JavaBean是我们常见的两个关键字&#xff0c;一般容易混淆&#xff0c;POJO全称是Plain Ordinary Java Object / Pure Old Jav…