注解配置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请求的时候所经历的坎坷之路: (书上原话,算是解释..) 在请求离开浏览器时① ,会带有用户所请 ...
随机推荐
- 二狗子 、初恋及HTTPS
最近二狗子宅在老家,最悠闲的就是泡壶茶看着院子的风景发呆一下午.今天,二狗子看到了对面自己暗恋的小翠花,看着美好的小翠花二狗子不禁想起了自己美好的初恋. 二狗子的初恋在初中,那个时候学校禁止带手机.上 ...
- PyObject and PyTypeObject - Python 中的 '对象' 们
1 PyObject, PyTypeObject - Python 中的 '对象' 们 '一切皆对象' - 这是 Python 的学习和使用者们最最常听到一句, 可谓 博大精深 - '勃大精深'. ' ...
- Python3(三) 变量与运算符
一.什么是变量 变量 = [1,2] 二.变量的命名规则 字母,数字,下划线,首字母不能是数字 系统关键字 不能用在变量名中 保留关键字 区别大小写 a=1, a='1', a=(1,2), ...
- JSON Hijacking实战利用
0×01漏洞的挖掘 一般挖掘的过程中,burpsuite代理的History做寻找,过滤多余不可能存在漏洞的一些链接,如下图所示: 我们在返回包中json格式发现了如下的敏感信息(用户Id,用户名,用 ...
- 11种常用css样式之border学习
边框border通常简写为"border:1px solid red;"但其实一个完整的border边框其实是由1.border-width/*边框宽度*/,2.border-st ...
- 前端的Cookies
Cookies cookies 特性 前端数据存储 后端通过 HTTP 头设置 请求时通过 HTTP 头传给后端 前端可读可写 遵守同源策略 域名 有效期 路径 http-only secure(ht ...
- 深入理解 Android 中的各种 Context
前言 网上关于 Context 的文章也已经有不少了,比如值得参考的有: Android Context完全解析,你所不知道的Context的各种细节 Android Context 到底是什么? 但 ...
- 14.Android-使用sendMessage线程之间通信
1.Handler介绍 Handler 是一个消息分发对象.handler是Android给我们提供用来更新UI的一套机制,也是一套消息处理机制,通过它可以实现在不同线程之间传递消息 本章Handle ...
- mysql 的root 用户无法授权,navicat 远程授权提示1044解决方案
先看解决方案 #------------mysql root 用户无法赋权问题解决 -------- ,登录 mysql -u root -p ,use mysql; 选择mysql数据库 ,执行以下 ...
- 准备工作-Visual Studio 安装
说明 网上很多安装教程,等到自己有时间的时候再写一篇自己安装的详细步骤 安装参考(网络) https://blog.csdn.net/qq_33485434/article/details/78454 ...