//@SessionAttributes(value={"user"},types={String.class})
@Controller
public class SpringMVC {
public static String SUCCESS = "success";
/**
* 使用@RequestMapping注解来映射请求的URL
* @return
*/
@RequestMapping("/helloword")
public String testhello() {
System.out.println("helloword");
return "success";
}
/**
* 支持提交pojo 会根据name与pojo属性进行匹配 支持级联属性
* @param user
* @return
*/
@RequestMapping("/testUser")
public String testUser(User user) {
System.out.println("User:"+user.toString());
return "success";
}
/**
* 支持使用servlet原生api
* HttpServletReqyest
* HttpServletResponse
* HttpSession
* java.security.Prinipal
* Locale
* InputStream
* OutputStream
* Reader
* Writer
* @throws IOException
*/
@RequestMapping("testServletAPI")
public String testServletAPI(HttpServletRequest request,
HttpServletResponse response,
Writer out) throws IOException { System.out.println("testServletAPI:"+request+"\n"+response+"\n"+out);
out.write("hello writer");
out.write(1111);
return "success";
}
/**
* ModelAndView
* Map及Model
* @sessionAttributes
* @ModelAttribute
* 目标方法的返回值可以是ModelAndView类型其中包含视图和模型信息
*/
@RequestMapping("testModelAndView")
public ModelAndView testModelAndView() {
String viewName="success";
ModelAndView modelAndView = new ModelAndView(viewName);
modelAndView.addObject("time",new Date());
return modelAndView;
}
/**
* 目标方法可以添加map(也可以是Model类型或ModelMap类型)类型参数
* @param map
* @return
*/
@RequestMapping("testMap")
public String testMap(Map<String,Object> map) {
map.put("names",Arrays.asList("tom","jerry","mike"));
return "success";
}
/**
* map 默认在request域里不会装进Session域
* 用sessionAttributes在controller类上做注解使属性值进入session域
* 其括号内可放键、值 如上设置
* @SessionAttributes(value={"user"},types={String.class})
* 表示将 键 为"user" 或 值为String类型的 键值对放入session域
* 注意 :该注解只能放在类上
*/ @RequestMapping("testSessionAttributes")
public String testSessionAttributes(Map<String, Object> map) {
User user = new User(121,"tom","@qwqx.com");
map.put("user", user);
map.put("email", "@myemail.com");
return SUCCESS;
}
/**@RequestMapping("testModelAttribute")
* 如果数据库中的数据进行修改操作,默认的表单提交会new一个对象传回后台
* 如果规定某些属性无法修改,在表单里我们是不需要列出来的,而表单里我们不对属性进行赋值,
* 会造成对象属性为null,在写入数据库时造成麻烦,
* 当然我们可以用hidden赋值,但如果是私密属性又会有麻烦
* 在这里我们可以选择先从数据库插叙获取到对象,传送到页面,表单提交只是对属性值作出更改,而不是新建对象
*/
@RequestMapping("testModelAttribute")
public String testModelAttribute(@ModelAttribute("user")User user) {
System.out.println("修改:"+user);
return SUCCESS;
}
/**
* 由@ModelAttribute标记的方法会在每个目标方法执行之前被SpringMVC调用
* 在ModelAttribute修饰的方法中,放入到Map时的键需要和目标方法参数类型的第一个字母小写的字符串一致
* 可以在目标方法的参数上用@ModelAttribute("user") 用于指定获取对象时需要查找的键
* testModelAttribute(@ModelAttribute("user")User user)
*/
@ModelAttribute
public void getUser(@RequestParam(value="id",required=false) Integer id,
Map<String, Object> map) {
if(id!=null) {
// 假设user为数据库查询出来的对象
User user = new User(2,"Tom", "@tom.com");
System.out.println("数据库查询出来的对象:"+user.toString());
map.put("user",user);
}
}
@RequestMapping("testViewSourceAndViewResolver")
public String testViewSourceAndViewResolver() { return SUCCESS;
}
@RequestMapping("testView")
public String testView() {
System.out.println("testView");
return "helloView";
}
@RequestMapping("testRedirect")
public String testRedirect() {
System.out.println("testRedirect");
return "redirect:/index.jsp";//重定向
}
}
 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="helloword">Hello Word</a>
<br>
<form action="testUser">
id:<input type="text" name="id"/>
<br>
userName:<input type="text" name="userName"/>
<br>
email:<input type="text" name="email"/>
<br>
provience:<input type="text" name="address.provience"/>
<br>
city:<input type="text" name="address.city"/>
<input type="submit" value="提交"/>
</form>
<br/>
<a href="testServletAPI">testServletAPI</a>
<br>
<a href="testModelAndView">testModelAndView</a>
<br>
<a href="testMap">test Map</a>
<br>
<a href="testSessionAttributes">testSessionAttributes</a> <br><br>
<!--
模拟修改操作
1.原始数据为 1 tom @tom.com
2.名字不能被修改
3.表单回显,模拟操作直接在表单填写对应的属性值
-->
<form action="testModelAttribute">
<input type="hidden" name = "id" value="1"/>
email:<input type="text" name ="email" value="@tom.com"/>
<input type="submit" value="提交"/>
</form>
<br><br> <a href="testViewSourceAndViewResolver">testViewSourceAndViewResolver</a> <br><br>
国际化:
<br>
<fmt:message key="i18n.username"></fmt:message>
<br>
<fmt:message key="i18n.password"></fmt:message> <br><br>
<a href="testView">testView</a>
<br><br>
<a href=testRedirect>testRedirect</a> <br><br>
<a href="i18n?locale=zh_CH">中文</a>
<a href="i18n?locale=en_US">英文</a>
<br><br> </body>
</html>

自定义视图

 @Component
public class HelloView implements View{ @Override
public String getContentType() {
return "text/html";
} @Override
public void render(Map<String, ?> arg0, HttpServletRequest arg1, HttpServletResponse arg2) throws Exception {
arg2.getWriter().print("hello view .time"+new Date());
} }

springmvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置自定义扫描得包 -->
<context:component-scan base-package="org.handler"></context:component-scan>
<!-- 配置视图解析器:如何把handler返回值解析为实际的物理视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 自定义图 视图解析器 使用视图的名字来解析视图-->
<!-- 通过order属性来定义视图优先级 order值越小优先级越高-->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="100"></property>
</bean>
<!-- 配置直接转发的页面 -->
<mvc:view-controller path="/success" view-name="success"/>
<!-- 在实际开发中需要配置mvc:annotation-driven标签 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置国际化资源文件 -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n"></property>
</bean>
</beans>

SpringMVC_1的更多相关文章

  1. Mybaits+SpringMVC项目(含代码生成工具源码)

       大家下载下来修改数据库配置应该就能运行起来,里面有一个SM的简单案例了,还有说明文件. 运行效果    工具类可以生成Springmvc+mybatis的相关类和配置文件,并具有增删查改的功能, ...

  2. 第八章.Spring MVC

    基于MyEclipse开发 工程结构: 所使用到的jar: 代码: FruitControllerTest.java public class FruitControllerTest implemen ...

随机推荐

  1. 51node 1134 最长递增子序列 (数据结构)

    题意: 最长递增子序列 思路: 普通的$O(n^2)$的会超时.. 然后在网上找到了另一种不是dp的写法,膜拜一下,自己写了一下解释 来自:https://blog.csdn.net/Adusts/a ...

  2. 树莓派 - platform总线,设备和驱动

    以树莓派为例子,分析一下其中LED的 platform device 和 platform driver. 查看LED设备,被挂载在/sys/devices/platform下. 注意其中的drive ...

  3. Wireshark does not show SSL/TLS

    why it doesn't show as "TLS/SSL"? Because it's not on the standard port for SSL/TLS. You c ...

  4. assert.throws()函数详解

    assert.throws(block[, error][, message]) Node.js FS模块方法速查 期望 block 函数抛出一个错误. 如果指定 error,它可以是一个构造函数.正 ...

  5. *** 红包书用法 及 ubuntu全局配置

    使用教程 http://go.wasai.org/sswiki https://home.maysoul.com/wiki/doku.php?id=shadowsocks ubuntu使用教程 htt ...

  6. 谷歌应用商店chrome扩展程序和APP的发布流程

    互联网上有很多大牛,他们再工作中需要一些难题,再找到解决办法后,如果会使用js的话,大多数人就可以自己动手写一个chrome插件,而且非常容易.开发人员都喜欢与大家分享自己的成就!google是一个全 ...

  7. LINUX系统---中级相关操作和知识

    LINUX系统的中级,来搞一些LINUX安全相关的东西,还有在公司生成中长搞的集群. RHCS集群 什么是高可用 什么是热备 什么是分布式

  8. scrapy快速入门

    1. 什么是scrapy? 其官网是这样简述的,“A Fast & Powerful Scraping &Crawling Framework ”,  并且其底层以twisted作为网 ...

  9. 【转载】什么是Zero-Copy

    转载:https://blog.csdn.net/u013256816/article/details/52589524 概述 考虑这样一种常用的情形:你需要将静态内容(类似图片.文件)展示给用户.那 ...

  10. express中间件的意思

    中间件就是请求req和响应res之间的一个应用,请求浏览器向服务器发送一个请求后,服务器直接通过request定位属性的方式得到通过request携带过去的数据,就是用户输入的数据和浏览器本身的数据信 ...