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 ...
随机推荐
- vue set方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- 在你的Android手机上运行Linux
之前试过许多方法(也就几种),像什么Complete Linux Installer,Debian noroot,利用已有的Linux构造Bootstrap之类,要么就是复杂得要命(调了两天没有调出来 ...
- CUDA_one
首先我看了讲解CUDA基础部分以后,大致对CUDA的基本了解如下: 第一:CUDA实行并行化的过程分为两部分,一个是线程块之间的并行(这是在每个线程网格中grid进行的),一个是对于每一个线程块内部各 ...
- LIBSVM使用方法及参数设置
LIBSVM 数据格式需要---------------------- 决策属性 条件属性a 条件属性b ... 2 1:7 2:5 ... 1 1:4 2:2 ... 数据格式转换--------- ...
- jQuery_计算器实例
知识点: fadeIn()---计算器界面载入淡入效果 hover()---鼠标移入移出某个元素时触发的事件 click()---鼠标单击事件 css()---对元素样式的操作 val()---获取表 ...
- Spring & Java
Spring & Java https://spring.io/ Spring Boot https://www.shiyanlou.com/courses/1152 Spring Boot入 ...
- Vue 实例以及生命周期
最简单的 Vue 实例 //html <div id="app"> {{message}} </div> //javascript var vm = new ...
- 序列终结者(bzoj 1521)
Description 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这 ...
- javaSctipt基础
===================================== JavaScript 脚本语言===================================== 什么是JavaSc ...
- Java Web 总结
Java Servlet 总结 Servlet 简介 Servlet 是什么? 运行在Web服务器上的应用程序,作为浏览器和服务器之间的中间层. 与CGI功能类似,优点在于 性能更好 在Web服务器的 ...