在随笔“springMVC项目配置文件”的基础上,进行优化,使用注解配置,控制器类得以简化:

一、注解配置springMVC

1、在HelloController类中,去除实现的Controller接口,并给方法HandlerRequest添加注解@RequestMapping:

  @Controller

  public Class HelloController{

    @RequestMapping("/hello")

    public ModelAndView HandlerRequest(HttpServletRequest x1,HttpServletResponse x2)    {       }

  }

  //其中,@Controller注解是用来声明控制器的

  //@RequestMapping注解表示/hello路径会映射到该方法上

  //另,若@RequestMapping注解作用在类上,则相当于给该类所有配置的映射地址前加上了一个地址

2、在dispatcher-servlet.xml 文件中,注释掉原来的映射设置:包括路径映射与控制器类的bean

重新增加一个组件扫描:  <context : component-scan base-package = "controller" />     //扫描controller包下的控制器类

二、配置视图解析器

  在WEB-INF 下建page文件夹,WEB-INF是javaweb默认的安全目录,不允许用户直接访问

1、在dispatcher-servlet.xml中,添加一个bean,告知视图解析器:

  <bean  id = "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name = "prefix" value = "/WEB-INF/page" />

    <property name = "subfix" value = ".jsp" />

  </bean>

  //配置springMVC内置的视图解析器InternalResourceViewResolver,在视图名上添加前缀后缀进而确定一个web应用中视图资源的物理路径

2、在HelloController中把方法HandlerRequest的方法体内容改为ModelAndView model = new ModelAndView(viewName:"index");

3、把原index.jsp文件剪切到WEB-INF 下page文件夹中

4、访问路径localhost/hello   则可以显示内容页了,实际路径是/WEB-INF/page/index.jsp

三、控制器接收请求数据

<form action = "/param" role = "form">

  用户名:<input type = "text"  name = "username"><br/>

  <input type = "submit" value = "提交">

</form>

servlet 原生ApI实现接收请求数据:

1、@RequestMapping("/param")

  public ModelAndView getparam(HttpServletRequest req, HttpServletResponse  resp){

    String username = req.getParameter("username");

    sysout(username);

    return null;

  }   //获取表单提交的用户名

2、同名匹配规则,把方法入参名设成与前台传参名一致,也可获取到表单提交的数据:

@RequestMapping("/param")

public ModelAndView getparam(String username){

    sysout(username);

    return null;

  }

//问题是,与前台强耦合

解决方法是用@RequestParam(“前台参数名”)来注入:

3、@RequestMapping("/param")

public ModelAndView getparam(@RequestParam("username")  String u){

    sysout(u);

    return null;

  } //同样获取到表单提交的数据

另,使用模型传参:前台参数名必须与模型字段名一致:

public Class  User{   String username;   //getter、setter方法}

public ModelAndView getparam(User user){

  sysout(user.getUsername);

  return null;

}

解决post方式的中文乱码问题: 通过在web.xml中添加springmvc字符编码过滤器:

<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>

//<filter><filter-mapping>要成对配置

四、控制器回显数据

/WEB-INF/page下创建test.jsp页面

<!Doctype html>

<%@page language = "java" contentType = "text/html;charset = UTF-8" pageEncoding = "UTF-8" import="java.util.*" isElIgnore = "false" %>  //不忽略el表达式

<html>

  <head>  <title>springmvc数据回显</title> </head>

  <body>

    <h1>  回显数据: ${ message }</h1>

  </body>

</html>

1、Servlet原生ApI回显数据:

@RequestMapping("/value")

public ModelAndView handlerRequest( HttpServletRequest req,  HttpServletResponse resp){

  req.setAttribute("message","成功");

  return new ModelAndView("test");

}  //localhost/value  返回: 回显数据:成功

2、使用springmvc的ModelAndView对象回显数据:

@RequestMapping("/value")

public ModelAndView handlerRequest( HttpServletRequest req,  HttpServletResponse resp){

  ModelAndView model  = new ModelAndView(viewName:"test");

  model.addObject(attributeName:"message", attributeValue:"成功");

  return model ;

}  // 访问 localhost/value  返回: 回显数据:成功

3、使用Model对象回显数据

@RequestMapping("/value")

public String handlerRequest(Model  m){

  m.addAttribute(s:"message",o:"成功");

  return  "test";

}

//@ModelAttribute注解

public void model(Model model){

  model.addAttribute("message","成功");

}

//message会放进页面参数中,在视图中直接调用。

客户端跳转

/hello 或者/test都是服务端的跳转,即:request.getRequestDispatcher("地址").forwards(req,resp);

改写HelloController控制器类:

@RequestMapping("/hello")

public ModelAndView handlerRequest(HttpServletRequest req, HttpServletResponse resp){

  ModelAndView model = new ModelAndView("index");

  model.addObject("message","hello springmvc");

  return model;

}

@RequestMapping("/jump")

public ModelAndView jump(){

  ModelAndView m = new ModelAndView("redirect:/hello");

  return m;

}

//访问localhost/jump 会自动跳转到  localhost/hello

结果和前面一样

也可以这样写:

@RequestMapping("/jump")

public String jump(){

  return "redirect: ./hello";

}

文件上传

先导入jar包: commons-io-1.3.2.jar      \     commons-fileupload-1.2.1.jar

1、在dispatcher-servlet.xml中配置上传解析器:

<bean id="multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" />

2、 upload.jsp建在page文件夹下:

<%@ page contentType = "Text/html; charset = UTF-8" language = "java" %>

<html>

  <head><title>文件上传</title><head>

  <body>

    <form action = "/upload" method = "post" enctype = "multipart/form-data">

      <input type = "file" name = "picture">

      <input type = "submit" value = "上传">

    </form>

  </body>

</html>

3、编写控制器类UploadController在包controller下:

@Controller

public class UploadController{

  @RequestMapping("/upload")

  public void upload(@Requestparam("picture") MultipartFile file) Throws Exception{

    sysout(file.getOriginalFileName());

  }

  @RequestMapping("/test2")

  public ModelAndView upload(){

    return new ModelAndView("upload")

  }

}

//访问 localhost/test2

注解配置springMVC的更多相关文章

  1. 注解配置springMvc及向作用域中赋值

    1.在applicationContext.xml中配置包扫描器 <!-- 使用注解配置扫描器 --> <context:component-scan base-package=&q ...

  2. 【SpringMVC】完全注解配置SpringMVC

    创建初始化类,代替web.xml 在Servlet3.0环境中,容器会在类路径中查找实现javax.servlet.ServletContainerInitializer接口的类,如果找到的话就用它来 ...

  3. springMVC学习记录2-使用注解配置

    前面说了一下使用xml配置springmvc,下面再说说注解配置.项目如下: 业务很简单,主页和输入用户名和密码进行登陆的页面. 看一下springmvc的配置文件: <?xml version ...

  4. spring mvc 第一天【注解实现springmvc的基本配置】

    创建pojo,添加标识类的注解@Controller,亦可添加该Handler的命名空间:设置类的@RequestMapping(value="/hr") 该类中的方法(Handl ...

  5. SpringMVC基础配置(通过注解配置,非xml配置)

    SpringMVC是什么,有多火,我这里就不再啰嗦了,SpringMVC比Struts2好用太多,我在学校的时候私下里两种都接触过,对比之后果断选择了SpringMVC,后来在做Android应用开发 ...

  6. SpringMVC整合mybatis基于纯注解配置

    Mybatis整合Spring配置 第一部分:配置Spring框架 配置SpringMVC的步骤 配置流程图 导入包(哪些包,基本包5个,1日志依赖包,2webmvc支持包)SpringMVC配置 & ...

  7. 关于什么是SpringMVC,和SpringMVC基于xml配置、注解配置、纯注解配置

    首先我们先要了解一下,什么是SpringMVC? SpringMVC是Spring框架内置的MVC的实现.SpringMVC就是一个Spring内置的MVC子框架,也就是说SpringMVC的相关包都 ...

  8. spring-mvc注解配置小记

    Controller中注解Service时,Service的实现类需加@Service,dao的实现类需加@Repository. 另:配置文件中对应的包也需要扫描到!!! <context:a ...

  9. SpringInAction--Spring Web应用之SpringMvc 注解配置

    Spring MVC 是当前Web服务器中常用的结构,今天就来学习这相关的知识,首先上图——Spring请求的时候所经历的坎坷之路: (书上原话,算是解释..) 在请求离开浏览器时① ,会带有用户所请 ...

随机推荐

  1. TestStand 基础知识[7]--Build-in Step Types (2)

    接着上一篇文章:TestStand 基础知识[6] Build-In StepTypes(1) 继续介绍: 还是先把Build-in StepTypes图片贴一下, 1. Call Executabl ...

  2. postfix 被当作垃圾邮件中转站

    磁盘 io 总是满的状态 该服务器只有监控和邮件elk在上面. 发现邮件日志 疯狂的输出 tail -f /var/log/maillog 大致都是来自于 yahoo.com.tw的东西 清空了 /v ...

  3. 在Unity中使用 luajit 64位加密

    参考:https://blog.csdn.net/sun19880421/article/details/68070696 https://blog.csdn.net/mydreamremindme/ ...

  4. 【转载】python_logging模块

    原文:https://www.cnblogs.com/liujiacai/p/7804848.html 1 logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日志 ...

  5. vue简单实现

    vue简单实现 vue的三个核心 虚拟dom, 双向绑定 Proxy,

  6. 《SQL基础教程》+ 《SQL进阶教程》 学习笔记

    写在前面:本文主要注重 SQL 的理论.主流覆盖的功能范围及其基本语法/用法.至于详细的 SQL 语法/用法,因为每家 DBMS 都有些许不同,我会在以后专门介绍某款DBMS(例如 PostgreSQ ...

  7. jvm 内存结构

    jvm 内存结构 graph TB A(jvm)-->E(类加载器系统) A-->B(运行时数据区) A-->D(本地库接口) A-->C(执行引擎) B-->虚拟机栈 ...

  8. Gartner评估:众包将掀起IT服务市场的革命

    国际IT顾问与咨询公司Gartner发布评估报告,称众包是中国的一种新兴业务模式,将掀起IT服务市场的革命.然而,只有很少的的服务提供商会构建众包平台来尝试使用该业务模式.IT服务提供商的业务部门负责 ...

  9. springCloud进阶(微服务架构&Eureka)

    springCloud进阶(微服务架构&Eureka) 1. 微服务集群 1.1 为什么要集群 为了提供并发量,有时同一个服务提供者可以部署多个(商品服务).这个客户端在调用时要根据一定的负责 ...

  10. WAF的基础绕过

    方法分类: 1.HTTP参数污染绕过 2.HTTP Header头部欺骗绕过 3.HTTP参数溢出绕过 4.HTTP分块传输绕过 5.HTTP数据编码绕过 6.HTTP Pipline绕过(Keep- ...