2019独角兽企业重金招聘Python工程师标准>>>

package org.base.controller;
import org.base.pojo.Department;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class DeptController {/*** 映射的url http://localhost:端口号/项目名/test.do* 跳转到 (prefix)test.jsp(suffix)页面*/@RequestMapping("/test")public String test(){System.out.println("test");return "test";}/*** 如果方法无返回值,则跳转到 testP.jsp页面,即映射名* 对于方法中的参数,可以对应url中的参数自动赋值* 这里注意,如果url中的参数和方法定义的不一致,例如,此处的age=1.5,那么会报400错误*/@RequestMapping("/testP")public void testParam(String name,Integer age){System.out.println("name:"+name+",age:"+age);}/*** 也可以显式声明url参数*/@RequestMapping("/testP1")public void testParam1(@RequestParam("name")String name,Integer age){System.out.println("name:"+name+",age:"+age);}/*** 自动注入bean属性,注意,url中形式是 departmentId=12,不是  对象.属性  形式*/@RequestMapping("/testP2")public void testParam2(Department dept){System.out.println("dept:"+dept);}
/*** ResponseBody注解:此方法返回值直接写入 response的body中,默认是json* 注意要导入  jackson.jar*/@RequestMapping("/test2")@ResponseBodypublic Department test2(){Department dept = new Department(12, "abc", 31, 5);return dept;} //@RequestMapping 注解的方法支持很多参数: HttpServletRequest/Response,HttpSession,Writer// 不支持对象.属性传值方法,如果重名,/*** restful 风格的传参方式*/@RequestMapping("/test3/{str}")public void test3(@PathVariable("str")String str){System.out.println("str:"+str);}/*** date 日期类型的注入*/@RequestMapping("/test4")public void test4(@DateTimeFormat(iso=ISO.DATE)Date d){if(d!=null)System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d));}
}

@ResponseBody 可以将该方法返回的值 写入到 response的body中去(String,json,xml)。

@RequestMapping 也可以用于整个类

@Controller
@RequestMapping("/emp/*")
public class EmpController {@RequestMapping("test1") // 这里匹配的url就是/emp/test1.dopublic void test1(String s){System.out.println("s:"+s);}
}

spring 配置文件

<?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:tx="
http://www.springframework.org/schema/tx
" xmlns:aop="
http://www.springframework.org/schema/aop
"xmlns:context="
http://www.springframework.org/schema/context
" xmlns:jee="
http://www.springframework.org/schema/jee
"xmlns:mvc="
http://www.springframework.org/schema/mvc
"xmlns:p="
http://www.springframework.org/schema/p
" xsi:schemaLocation="
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
"><!-- 加载配置文件 --><context:property-placeholder location="classpath:config/jdbc.properties" /><context:component-scan base-package="org.base" /><!-- 开启基于注解的mvc配置 --><mvc:annotation-driven><mvc:message-converters><bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"><!-- 解决"IE下返回json提示下载"问题 --><property name="supportedMediaTypes" value="text/plain;charset=UTF-8"></property></bean></mvc:message-converters></mvc:annotation-driven><!-- 视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"p:prefix="/WEB-INF/" p:suffix=".jsp"></bean>
</beans>

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><!-- spring mvc --><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/spring.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping>
</web-app>

参考:Spring MVC 框架搭建及详解

Spring mvc 返回JSON 在IE 下提示下载 解决办法

转载于:https://my.oschina.net/u/2250875/blog/383166

springmvc 使用相关推荐

  1. 【SpringMVC】概述

    概述: SpringMVC:是基于spring的一个框架, 实际上就是spring的一个模块, 专门是做web开发的.                       理解是servlet的一个升级 Sp ...

  2. 【SpringMVC】基本概念

    SpringMVC的基本概念 三层架构 我们的开发一般都是基于c/s或者b/s架构.在JavaEE开发中,几乎全都是基于B/S架构开发.在B/S架构中,系统标准的三层架构包括:表现层,业务层,持久层. ...

  3. SSM框架整合(Spring+SpringMVC+MyBatis)

    输出结果 1.Maven Web项目创建 之前有写过Eclipse+Maven创建web项目的帖子,如果需要,请参考这里写链接内容 创建好项目之后因为入下图: 2.SSM整合 2.1 引入需要的JAR ...

  4. SpringBoot-web开发(四): SpringMVC的拓展、接管(源码分析)

    [SpringBoot-web系列]前文: SpringBoot-web开发(一): 静态资源的导入(源码分析) SpringBoot-web开发(二): 页面和图标定制(源码分析) SpringBo ...

  5. SpringMVC——通俗易懂讲讲Ajax~

    聊聊Ajax 一.什么是Ajax 二.iframe标签简单伪造Ajax 三.Ajax的实现 1. 基本概念的了解 Ajax的核心是什么? XMLHttpRequest是什么? Ajax数据传输的数据格 ...

  6. Jackson、FastJson快速入门(整合SpringMVC)

    目录 1. 什么是 JSON 2. JSON 语法规则 3. JSON 与 JS 对象的关系 4. JSON 和 JS 对象互转 5. Jackson-数据格式转换 1. 环境搭建 2. 对象转jso ...

  7. RESTful风格及其SpringMVC实现

    目录 1.RESTful概念 2.RESTful功能 3.对比:传统方式操作资源 4.SpringMVC实现传统方式操作资源 5.使用RestFul操作资源 6.SpringMVC实现RESTful操 ...

  8. 使用注解开发SpringMVC详细配置教程

    目录 1.使用注解开发SpringMVC 1.新建一个普通的maven项目,添加web支持 2.在pom.xml中导入相关依赖 3.配置web.xml 4.编写SpringMVC配置文件 1. 自动扫 ...

  9. SpringMVC的form:form表单的使用

    为什么要使用SpringMVC的form:form表单,有两个原因:一是可以更加快捷的完成表单的开发,比如会替你做好数据类型装换等本来需要你自己动手的工作.其次就是能够更加方便的实现表单回显. 首先要 ...

  10. SpringMVC工作环境搭建 配置文件

    web.xml配置 在服务器端容器启动之前加载配置文件的顺序:context-param>listener>filter>servlet //容器配置application上下文的时 ...

最新文章

  1. ping -c3 baidu.com  ping过去是这样,代表网络畅通
  2. 第四周项目二-太乐了
  3. POJ 1753 Flip Game (黑白棋) (状态压缩+BFS)
  4. centos安装vsftpd
  5. linux充当防火墙,Linux下主机充当防火墙的巧妙应用之iptables!.doc
  6. Linux tcpdump命令用法详解
  7. Springboot启动报错:DEBUG org.springframework.boot.diagnostics.FailureAnalyzers
  8. 建阳有计算机学校吗,建阳有哪几所中专技校
  9. linux系统工程师修改打开文件数限制代码教程。服务器运维技术
  10. AS中XML注释和取消注释快捷键,实际操作真实有效!!!
  11. Android 签名打包
  12. 生成PDF并上传到图片服务器
  13. linux脚本看日历,Linux查看日历之cal命令
  14. R语言绘制花瓣图flower plot
  15. 企业微信H5_自建应用连接H5
  16. 微信小程序 - 修改 button 边框和背景色
  17. 一种实现ISA/IEC 62443操作技术标准的零信任应用简化模型
  18. mycat原理及分表分库入门
  19. 田东县谋定产业格局-农业大健康·林裕豪:从玉农业携手推动
  20. 根据SQL必知必会学习SQL(MYSQL)

热门文章

  1. Layui 数据表格复杂表头
  2. Python Selenium打开谷歌浏览器
  3. solr5.0mysql_solr5.5.4 添加mysql数据,实现同步更新
  4. LINUX给进程内容窗口改名的代码
  5. C盘空间丢失30G,怎么也找不到
  6. grep有时抓不到文本怎么办?
  7. 强烈抗议故意审核不通过
  8. 坐飞机还是尽量早点出发(差点误机)
  9. 为枪击事件默哀,程序员们确实要重视代码规范
  10. python生成exe文件太大了_Pyinstaller打包生成exe文件过大,四种常用处理方法集锦---嵌入式Python-02...