注解配置springMVC
在随笔“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的更多相关文章
- 注解配置springMvc及向作用域中赋值
1.在applicationContext.xml中配置包扫描器 <!-- 使用注解配置扫描器 --> <context:component-scan base-package=&q ...
- 【SpringMVC】完全注解配置SpringMVC
创建初始化类,代替web.xml 在Servlet3.0环境中,容器会在类路径中查找实现javax.servlet.ServletContainerInitializer接口的类,如果找到的话就用它来 ...
- springMVC学习记录2-使用注解配置
前面说了一下使用xml配置springmvc,下面再说说注解配置.项目如下: 业务很简单,主页和输入用户名和密码进行登陆的页面. 看一下springmvc的配置文件: <?xml version ...
- spring mvc 第一天【注解实现springmvc的基本配置】
创建pojo,添加标识类的注解@Controller,亦可添加该Handler的命名空间:设置类的@RequestMapping(value="/hr") 该类中的方法(Handl ...
- SpringMVC基础配置(通过注解配置,非xml配置)
SpringMVC是什么,有多火,我这里就不再啰嗦了,SpringMVC比Struts2好用太多,我在学校的时候私下里两种都接触过,对比之后果断选择了SpringMVC,后来在做Android应用开发 ...
- SpringMVC整合mybatis基于纯注解配置
Mybatis整合Spring配置 第一部分:配置Spring框架 配置SpringMVC的步骤 配置流程图 导入包(哪些包,基本包5个,1日志依赖包,2webmvc支持包)SpringMVC配置 & ...
- 关于什么是SpringMVC,和SpringMVC基于xml配置、注解配置、纯注解配置
首先我们先要了解一下,什么是SpringMVC? SpringMVC是Spring框架内置的MVC的实现.SpringMVC就是一个Spring内置的MVC子框架,也就是说SpringMVC的相关包都 ...
- spring-mvc注解配置小记
Controller中注解Service时,Service的实现类需加@Service,dao的实现类需加@Repository. 另:配置文件中对应的包也需要扫描到!!! <context:a ...
- SpringInAction--Spring Web应用之SpringMvc 注解配置
Spring MVC 是当前Web服务器中常用的结构,今天就来学习这相关的知识,首先上图——Spring请求的时候所经历的坎坷之路: (书上原话,算是解释..) 在请求离开浏览器时① ,会带有用户所请 ...
随机推荐
- java架构之路-(微服务专题)nacos集群精讲实战
上次回顾: 上次博客,我们主要说了微服务的发展历程和nacos集群单机的搭建,单机需要-m standalone启动,集群建议使用nginx做一下反向代理,自行保证mysql和ngxin的高可用. 本 ...
- 关于线段树的感悟(Segment Tree)
线段树的感悟 : 学过的东西一定要多回头看看,不然真的会忘个干干净净. 线段树的 Introduction : English Name : Segment Tree 顾名思义 : 该数据结构由两个重 ...
- jQuery 基础 (笔记源于runoob)
您需要具备的基础知识 在您开始学习 jQuery 之前,您应该对以下知识有基本的了解: HTML CSS JavaScript jQuery ? jQuery是一个JavaScript函数库. jQu ...
- Lnmp环境源码包编辑安装
最近做了一个小工具可以方便的部署LNMP环境,有兴趣的同学可以尝试下: 这是一个集成的shell脚本,脚本将会自动安装好LNMP环境相关软件: 使用步骤 1.下载脚本源码到本地 git clone h ...
- Nginx总结(八)Nginx服务器的日志管理及配置
前面讲了如何配置Nginx虚拟主机,大家可以去这里看看nginx系列文章:https://www.cnblogs.com/zhangweizhong/category/1529997.html 今天要 ...
- 【笔记】Git简明教程
前言 Git这个东西我曾经有学过,但学的内容太多了,有点懵,不太理解,磕磕碰碰的,走了不少弯路.不过最近我在B站上发现了一个讲的很好的教程:<表严肃讲Git>.因此,我决定用文字的方式分享 ...
- Linux的那些事-系统启动(增加开机启动项)
1 /etc/init.d 2 /etc/inittab 3 /etc/rc.d/init.d 1. /etc/init.d 是一般开机的启动服务存放在这个目录下,至于实现机制,其实 ...
- es5实现一个class
es5实现一个class https://juejin.im/post/5ac1c5bf518825558949f898#heading-9
- js类的constructor中不支持异步函数吗?
解决方案: 1.如果是普通函数,可以用async 和await来解决你的问题但你这个是在constructor里,constructor 的作用是返回一个对像实例,如果加了async就变成返回一个pr ...
- linux学习--1. 文件系统
文件目录结构 闲话篇: linux我也是最近才开始学,写随笔是为分享学习经验的同时也留着供自己以后来参考.因为linux一切皆文件的基本哲学思想.所以我决定从文件目录开始写. 正文: 首先linux文 ...