SpringMVC_1
//@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的更多相关文章
- Mybaits+SpringMVC项目(含代码生成工具源码)
大家下载下来修改数据库配置应该就能运行起来,里面有一个SM的简单案例了,还有说明文件. 运行效果 工具类可以生成Springmvc+mybatis的相关类和配置文件,并具有增删查改的功能, ...
- 第八章.Spring MVC
基于MyEclipse开发 工程结构: 所使用到的jar: 代码: FruitControllerTest.java public class FruitControllerTest implemen ...
随机推荐
- python爬取网页图片
# html:网页地址 def getImg2(html): soup = BeautifulSoup(html, 'html.parser') href_regex = re.compile(r'^ ...
- 笔试算法题(33):烙饼排序问题 & N!阶乘十进制末尾0的个数二进制最低1的位置
出题:不同大小烙饼的排序问题:对于N块大小不一的烙饼,上下累在一起,由于一只手托着所有的饼,所以仅有一只手可以翻转饼(假设手足够大可以翻转任意块数的 饼),规定所有的大饼都出现在小饼的下面则说明已经排 ...
- wamp下mysql错误提示乱码的解法
出处:http://blog.csdn.net/jsship/article/details/42914217 运行mysql命令时,出现的错误提示是乱码 : [Err] 1064 - Erre ...
- IIS 注册.NET Framework 4.0 命令
cmd执行以下命令 32位Windows:C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i 64位Windows:C ...
- c网购物车流程图
1. 流程图 2. 流程介绍 1) 客人浏览模式下(未登录状态)加入购物车 这个时候回校验一下商品的可售数量,以及状态等等,校验成功后会保存到cookie和memcache,数据操作校验以memcac ...
- Android SwipeSelector
Android SwipeSelector Android SwipeSelector是github上一个第三方开源的项目,其项目主页:https://github.com/roughike/S ...
- Spark 动态(统一)内存管理模型
作者编辑:王玮,胡玉林 一.回顾 在前面的一篇文章中我们介绍了spark静态内存管理模式以及相关知识https://blog.csdn.net/anitinaj/article/details/809 ...
- ELK pipeline
https://www.felayman.com/articles/2017/11/24/1511527532643.html?utm_medium=hao.caibaojian.com&ut ...
- CODEVS——T 3736 【HR】万花丛中2
http://codevs.cn/problem/3736/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description ...
- Hive之执行计划分析(explain)
Hive是通过把sql转换成对应mapreduce程序,然后提交到Hadoop上执行,查看具体的执行计划可以通过执行explain sql知晓 一条sql会被转化成由多个阶段组成的步骤,每个步骤有执行 ...