---恢复内容开始---

web.xml:

    

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!--处理编码格式的问题-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--编码格式结束-->
</web-app>

dispatcher-servlet.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置 Spring的注解 扫包-->
<context:component-scan base-package="com.hxzy"></context:component-scan>
<!--配置SpringMVC的视图 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--配置国际化显示工具-->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 指定文件基名 -->
<property name="basename" value="message"/>
<!-- 当没有找到资源文件时,用这基名文件 -->
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<!--配置适配 MVC基本配置-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置SpringMVC 不经过Handler 直接访问jsp 拦截器必须是 / 不能是 *.do-->
<mvc:view-controller path="A/gugugu" view-name="success"></mvc:view-controller>
<!--配置静态资源访问权限 需要跟<mvc:annotation-driven> 配合使用-->
<mvc:default-servlet-handler></mvc:default-servlet-handler> <!--配置类的转换器 1.将自定义的转换器加载到Bean-->
<bean id="converter" class="com.hxzy.test.MyConverter"></bean>
<!--3.结束 此配置是SpringMVC的基础配置,很功能都需要通过该注解来协调 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!-- 配置 数据格式化 注解 所依赖的bean 2.将自定义的Bean 加入到SpringMVC提供的Bean中 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="converter"></ref>
</set>
</property>
</bean> </beans>

TestSpringMVC:

  

package com.hxzy.test;
import com.hxzy.pojo.Address;
import com.hxzy.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;
//@SessionAttributes("student") //放在Session作用域中 可以是type= "Student.class" 或者是 key的值
@RequestMapping("/SpringMvc")
@Controller //将该类加载到SpringIOC容器
public class TestSpringMVC {
/**
* 普通方法操作
* @return
*/
@RequestMapping("show1")
public String show1(){
System.out.println("进入show1");
return "success";
}
/**
* 普通的POST方式
*/
@RequestMapping(value = "show2",method = RequestMethod.POST)
public String show2(){
return "success";
}
/**
* name 属性的值必须是zs 并且 年龄不能是25岁(也可以没有这个age 属性) 并且 不能包含sex属性 ()
*/
@RequestMapping(value = "show3",method = RequestMethod.POST,params = {"user=zs","age!=25","!sex"})
public String show3(){
return "success";
}
/**
* 参数要求 包含一个参数 并获取他
*/
@RequestMapping(value = "show4/{name}",method = RequestMethod.POST)
public String show4(@PathVariable("name") String name){
System.out.println(name);
return "success";
}
/**
* 获取 通过普通的方式获取表单里面的值
*
* 多个方式的话 在方法里面填写就可以了
* 为了处理 前台页面传来的值小于获取的值需要设置
* (前台传来了3个值 可是Handler里面接受 5个值)
* @RequestParam(value = "age",required = false,defaultValue ="25") Integer age)
* 需要这样写
*/
@RequestMapping(value = "show_form",method = RequestMethod.POST)
public String show_from(@RequestParam("user") String name ,@RequestParam(value = "age",required = false,defaultValue ="25") Integer age){
System.out.println("name="+name);
System.out.println("age=" + age);
return "success";
} /**
* 使用SpringMVC的核心功能 最牛逼的地方 将前台的数据传输到实体类
* 注意属性一定要对应
*/
@RequestMapping(value = "show_allCanshuw",method = RequestMethod.POST)
public String show_allCanshuw(Student student){
System.out.println(student);
return "success";
}
/**
* 使用原生Servlet
*/
@RequestMapping(value = "use_servlet",method = RequestMethod.POST)
public String use_servlet(HttpServletRequest request){
System.out.println(request);
return "success";
}
/**
* 返回数据和视图 使用 ModelAndView
*/
@RequestMapping(value = "show_student")
public ModelAndView show_student(){
System.out.println("进入ModelAndView.........");
ModelAndView mv = new ModelAndView();
mv.setViewName("success");
Student student = new Student();
student.setS_age(15);
student.setS_name("大黄");
mv.addObject("student",student);
return mv;
}
/**
* 返回数据类型 Map
*/
@RequestMapping("show_map")
public String show_map(Map<String,Object> map){
Student student = new Student();
student.setS_name("古斌map");
map.put("studentMap",student);
return "success";
}
/**
* 返回数据类型 Model
*/
@RequestMapping("show_model")
public String show_model(Model model){
Student student = new Student();
student.setS_name("古斌Model");
model.addAttribute("studentModel",student);
return "success";
}
/**
* 返回数据类型 Modelmap
*/
@RequestMapping("show_ModelMap")
public String show_ModelMap(ModelMap modelMap){
Student student = new Student();
student.setS_name("古斌ModelMap");
modelMap.put("studentModelMap",student);
return "success";
}
/**
* 修改数据 从数据库读出来的数据 进行修改 跟下面的方法对应!!!
*/
@RequestMapping(value = "updateStudent",method = RequestMethod.POST)
public String updateStudent(@RequestParam("updatename") String name,@ModelAttribute("s") Student student){
student.setS_name(name);
System.out.println(student);
return "success";
} //为了配合修改 加上ModelAttribute
@ModelAttribute
public void queryStudent(Map<String,Object> map){
System.out.println("ModelAttribute开始执行");
//假设数据是从数据库查看的
Student student = new Student();
student.setS_name("古斌");
student.setS_age(18);
Address address = new Address();
address.setSchool_address("石家庄");
System.out.println(student);
map.put("s",student); //如果 Map的key 跟修改的方法的参数对不上 需要在参数 加上@ModelArrribute
}
/**
* 国际化处理
*/
@RequestMapping("i18n_test")
public String i18n_test(){
return "success";
}
/**
* 测试指定方式请求 转发||重定向
*
* 通过“forward:”指定跳转方式为请求转发
* 通过“redirect:”指定跳转方式为重定向
*/
@RequestMapping("SetHttp")
public String SetHttp(){
return "redirect:/view/success.jsp";
}
/**
* 配置类的转换器 String 古斌-18 转换 成 Student对象
*/
@RequestMapping("StudentInfo")
public String StudentInfo(@RequestParam("studentinfo") Student student){
System.out.println(student);
return "success";
}
/**
* 配置格式化的类 日期格式化
*/
@RequestMapping("Studentinfoss")
public String Studentinfoss(Student student ,BindingResult result){
//如果有错误信息
if (result.getErrorCount() > 0)
{
//循环遍历所有错误信息
for (FieldError error : result.getFieldErrors())
{
System.out.println(error.getField() + ":"
+ error.getDefaultMessage());
}
}
System.out.println(student);
return "success";
}
/**
* 数据的校验 演示 校验Email
*
* @Null 被注释的元素必须为 null。
* @NotNull 被注释的元素必须不为 null。
* @AssertTrue 被注释的元素必须为 true。
* @AssertFalse 被注释的元素必须为 false。
* @Min(value) 被注释的元素必须是一个数字,其值必须大于或等于value。
* @Max(value) 被注释的元素必须是一个数字,其值必须小于或等于value。
* @DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于或等于value。
* @DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于或等于value。
* @Size(max, min) 被注释的元素的取值范围必须是介于min和max之间。
* @Digits (integer, fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内。
* @Past 被注释的元素必须是一个过去的日期。
* @Future 被注释的元素必须是一个将来的日期。
* @Pattern(value) 被注释的元素必须符合指定的正则表达式。
* Hibernate Validator 是JSR 303的扩展。Hibernate Validator 提供了 JSR 303中所有内置的注解,以及自身扩展的4个注解,如下:
*
* 注解 简介
* @Email 被注释的元素值必须是合法的电子邮箱地址。
* @Length 被注释的字符串的长度必须在指定的范围内。
* @NotEmpty 被注释的字符串的必须非空。
* @Range 被注释的元素必须在合适的范围内。
*
*/
@RequestMapping("Jiaoyan")
public String Jiaoyan(@Valid Student student, BindingResult result){
//如果有错误信息
if (result.getErrorCount() > 0) {
//循环遍历所有错误信息
for (FieldError error : result.getFieldErrors())
{
System.out.println( ":"+ error.getDefaultMessage()+error.getField());
}
}
System.out.println(student);
return "success";
} }

index.jsp

<%--
Created by IntelliJ IDEA.
User: 古斌
Date: 2019/3/14
Time: 17:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>测试类</title>
</head>
<body>
<%--普通方式--%>
<a href="SpringMvc/show1.do">Get方式指定</a><br>
<%--post模式--%>
<form action="SpringMvc/show2.do" method="post">
<input type="submit" value="普通的POST方式">
</form>
<hr>
<%--name属性必须等于zs--%>
<form action="SpringMvc/show3.do" method="post">
账号 <input name="user">年龄<input name="age">
<input type="submit">
</form>
<hr>
<%--有存取一个姓名 如果有.do .action 都写在名字后面--%>
<form action="SpringMvc/show4/古斌.do" method="post">
<input type="submit" value="获取姓名">
</form>
<hr>
<%--获取name的值 普通方式操作--%>
<form action="SpringMvc/show_form.do" method="post">
<input name="user">
<input type="submit" value="普通方式查询name的值">
</form>
<hr>
<%--测试Spring MVC最牛逼之处 将前台表单的数据 直接封装到实体类--%>
<form action="SpringMvc/show_allCanshuw.do" method="post">
姓名 <input name="s_name"> 年龄<input name="s_age"> 地址<input name="address.school_address">
<input type="submit" value="提交_封装到实体类">
</form>
<hr>
<%--原生Servlet--%>
<form action="SpringMvc/use_servlet.do" method="post">
<input type="submit" value="使用原生Servlet">
</form>
<hr>
<%--获取requeest作用域中的数据--%>
<form action="SpringMvc/show_student.do" method="post">
<input type="submit" value="ModelAndView方式">
</form>
<hr>
<%--使用Map集合方式实现数据交互--%>
<a href="SpringMvc/show_map.do">Map方式</a> <hr>
<%--使用ModelMap--%>
<a href="SpringMvc/show_ModelMap.do">ModelMap方式</a><hr>
<%--使用Model--%>
<a href="SpringMvc/show_model.do">Model方式</a><hr>
<%--修改数据--%>
<form method="post" action="SpringMvc/updateStudent.do">
name; <input type="text" name="updatename">
<input type="submit" value="新修改">
</form><hr>
<%--国际化显示数据--%>
<a href="SpringMvc/i18n_test.do">i18N</a><hr>
<%--不经过Handler处理直接进入数据 这个web.xml中的Spring配置必须是/ 不能是*.do 等--%>
<a href="A/gugugu">NoApplicable_</a><hr>
<%--配置修改请求方式 转发 重定向 这种方式对国际化会有影响 导致 国际化代码不能使用--%>
<a href="SpringMvc/SetHttp.do">转发or重定向</a><hr>
<%--配置转换器--%>
<form method="post" action="SpringMvc/StudentInfo.do">
信息; <input type="text" name="studentinfo">
<input type="submit" value="转换">
</form><hr>
<%--配置日期格式化--%>
<form method="post" action="SpringMvc/Studentinfoss.do">
日期; <input type="text" name="birthday">
姓名:<input type="text" name="s_name"/><br>
年龄:<input type="text" name="s_age"/><br>
<input type="submit" value="显示日期">
</form><hr>
<%--数据的校验--%>
<form method="post" action="SpringMvc/Jiaoyan.do">
日期; <input type="text" name="birthday"><br>
姓名:<input type="text" name="s_name"/><br>
年龄:<input type="text" name="s_age"/><br>
Email:<input name="email"> <br>
<input type="submit" value="显示日期">
</form><hr>
</body>
</html>

  

Student.java

package com.hxzy.pojo;

import lombok.Data;
import org.hibernate.validator.constraints.Email;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat; import java.util.Date; @Data
public class Student {
private String s_name;
@NumberFormat(pattern = "##,#")
private Integer s_age;
@DateTimeFormat(pattern="yyyy-MM-dd")//格式化:前台传递来的数据,将前台传递来到数据 固定为yyyy-MM-dd
private Date birthday ;// 2018-12-13
@Email
private String email; @Override
public String toString() {
return "Student{" +
"姓名='" + s_name + '\'' +
", 年龄=" + s_age +
"日期"+birthday+
// ", 地址=" + address.getSchool_address() +
'}';
}
}

SpringMVC常用方法大全的更多相关文章

  1. JQUERY 常用方法大全

    Attribute: $("p").addClass(css中定义的样式类型); 给某个元素添加样式 $("img").attr({src:"test ...

  2. Python的数据类型和常用方法大全

    数据类型 一.数字 整形int x=10 #x=int(10) print(id(x),type(x),x) 浮点型float salary=3.1 #salary=float(3.1) print( ...

  3. SpringMVC常用方法总结

    *) @RequestMapping(value="/xxx/{id}",method={RequestMethod.GET}) method 不写的话,默认GET.POST都支持 ...

  4. Python读写EXCEL文件常用方法大全

    前言 python读写excel的方式有很多,不同的模块在读写的讲法上稍有区别,这里我主要介绍几个常用的方式. 用xlrd和xlwt进行excel读写: 用openpyxl进行excel读写: 用pa ...

  5. springBoot注解大全JPA注解springMVC相关注解全局异常处理

    https://www.cnblogs.com/tanwei81/p/6814022.html 一.注解(annotations)列表 @SpringBootApplication:包含了@Compo ...

  6. Spring SpringMVC SpringBoot SpringCloud 注解整理大全

    Spring SpringMVC SpringBoot SpringCloud 注解整理 才开的博客所以放了一篇以前整理的文档,如果有需要添加修改的地方欢迎指正,我会修改的φ(๑˃∀˂๑)♪ Spri ...

  7. springmvc 多数据源 SSM java redis shiro ehcache 头像裁剪

    获取下载地址   QQ 313596790  A 调用摄像头拍照,自定义裁剪编辑头像 B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单;  技术:31359679 ...

  8. java springMVC SSM 操作日志 4级别联动 文件管理 头像编辑 shiro redis

    A 调用摄像头拍照,自定义裁剪编辑头像 B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单;  技术:313596790freemaker模版技术 ,0个代码不用写 ...

  9. 【机器学习Machine Learning】资料大全

    昨天总结了深度学习的资料,今天把机器学习的资料也总结一下(友情提示:有些网站需要"科学上网"^_^) 推荐几本好书: 1.Pattern Recognition and Machi ...

随机推荐

  1. Axure知识点

    1 一个事件包含N个用例:一个用例包含N个动作.

  2. DropDownList控件的使用方法

    1. 使用代码添加数据 <asp:DropDownList ID="DropDownList1" runat="server"> </asp: ...

  3. oracle忘记密码,修改密码

    1:输入命令: sqlplus /nolog ,进入oracle控制台,并输入 conn /as sysdba;以DBA角色进入.2:若修改某一个用户密码, 修改用户口令 格式为:alter user ...

  4. golang之map数据类型

    先上代码…… package main import "fmt" func testMap() { //两种声明map方式,切记,必须初始化才能用,否则panic //var a ...

  5. php识别二维码

    php-zbarcode 是 PHP 读取条形码的扩展模块,目前仅支持 php5.x

  6. Java 设计模式系列(九)组合模式

    Java 设计模式系列(九)组合模式 将对象组合成树形结构以表示"部分-整体"的层次结构.组合模式使得用户对单个对象的使用具有一致性. 一.组合模式结构 Component: 抽象 ...

  7. SSL握手通信详解及linux下c/c++ SSL Socket代码举例(另附SSL双向认证客户端代码)

    SSL握手通信详解及linux下c/c++ SSL Socket代码举例(另附SSL双向认证客户端代码) 摘自: https://blog.csdn.net/sjin_1314/article/det ...

  8. LCD相关基础知识

    1.什么是LCD? (1)LCD(Liquid Crystal Display)俗称液晶.液晶是一种材料,液晶这种材料具有一种特点:可以在电信号的驱动下液晶分子进行旋转,旋转时会影响透光性, 因此我们 ...

  9. selenium设置代理,基于chrome浏览器

    工作中遇到需要对项目中使用的selenium设置代理,跟大家分享一下. 1.下载chromeDriver:http://chromedriver.storage.googleapis.com/inde ...

  10. 关于wcf配置未启动net.tcp监控导致无法访问wcf

    在服务里面启动NetTcpActivator和NetTcpPortSharing服务