最后
Java架构进阶面试及知识点文档笔记
这份文档共498页,其中包括Java集合,并发编程,JVM,Dubbo,Redis,Spring全家桶,MySQL,Kafka等面试解析及知识点整理
Java分布式高级面试问题解析文档
其中都是包括分布式的面试问题解析,内容有分布式消息队列,Redis缓存,分库分表,微服务架构,分布式高可用,读写分离等等!
互联网Java程序员面试必备问题解析及文档学习笔记
Java架构进阶视频解析合集
(5)视图解析器:View Resolver [rɪ’zɒlvə]
View Resolver 负责将处理结果生成 View 视图,View Resolver 首先根据逻辑视图名解析成物理视图名,即具体的页面地址,再生成 View 视图对象,最后对 View 进行渲染将处理结果通过页面展示给用户。
(6)视图:View
SpringMVC 框架提供了很多的 View 视图类型的支持,包括:jstlView、freemarkerView、pdfView等。最常用的视图就是 jsp。一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程序员根据业务需求开发具体的页面
===================================================================================
1、引入相关依赖
junit
junit
4.11
test
org.springframework
spring-core
4.3.2.RELEASE
org.springframework
spring-context
4.3.2.RELEASE
org.springframework
spring-context-support
4.3.2.RELEASE
org.springframework
spring-jdbc
4.3.2.RELEASE
org.springframework
spring-aop
4.3.2.RELEASE
org.springframework
spring-beans
4.3.2.RELEASE
org.springframework
spring-expression
4.3.2.RELEASE
org.springframework
spring-aspects
4.3.2.RELEASE
org.springframework
spring-tx
4.3.2.RELEASE
org.springframework
spring-web
4.3.2.RELEASE
org.springframework
spring-webmvc
4.3.2.RELEASE
javax.servlet
servlet-api
2.5
provided
2、编写springmvc配置文件(applicationContext.xml)
<context:component-scan base-package=“com.tjcu”></context:component-scan>
3、配置springmvc的核心Servlet
mvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
mvc
/
-
注意 :
这里还要加载spring配置,通过在servlet写initparam标签,还是contextConfigLocation属性,value用来加载springmvc配置文件
-
Servlet默认单例,在第一个请求的时候创建,如果不想要写
<init-param>
,可以把配置文件放在Web-INF
下,并命名为mvc-servlet.xml
4、创建控制器
//负责控制层组件对象创建
@Controller(“userController”)
//类上的控制器映射器,相当于Struts2中的namespace
@RequestMapping(“user”)
public class UserController{
//方法上的控制器映射器,相当于Struts中的name
@RequestMapping(“queryAll”)
public String queryAll(){
System.out.println(“查询到了很多数据”);
//解析结果:前缀+返回值+后缀
return “index”;
}
-
@Controller
: 该注解用来类上标识这是一个控制器组件类并创建这个类实例 -
@RequestMapping
:
修饰范围: 用在方法或者类上
注解作用: 用来指定类以及类中方法的请求路径
注解详解:
用在类上相当于struts2中namespace在访问类中方法必须先加入这个路径
用在方法上相当于action标签的name属性用来表示访问这个方法的路径
5.部署项目,启动项目测试
访问路径: http://localhost:8080/SpringMvc/user/queryAll
6、SpringMVC的开发步骤总结
①导入SpringMVC相关坐标(spring,servlet-api,spring-mvc等)
②配置SpringMVC核心控制器DispathcerServlet
③创建Controller类和视图页面
④使用注解配置Controller类中业务方法的映射地址
⑤配置SpringMVC核心文件 springmvc.xml(三大核心组件)
⑥客户端发起请求测试
===================================================================================
1、跳转方式
说明:跳转有两种=方式,一种forward ,一种**redirect ** [ˌriːdəˈrekt ]。
(1)Servlet跳转方式【底层】
#forward跳转,
一次请求,地址栏不变,可以使用request作用域传递数据,
#redirect跳转多次,
地址栏改变,不能使用作用域
(2)Struts2中的跳转方式(区分跳转的目标资源类型)
#Action—>jsp
forward[默认] type=“dispatcher”
redirect type=“redirect”
#Action—>Action
forward type=“chain”
redirect type=“redirectAction”
(3)springmvc跳转方式
1.Controller 跳转到JSP
forward跳转到页面: 默认就是forward跳转
语法:return “业务逻辑名”
例子:return “showUer”---->/showUser.jsp
redirect跳转到页面:使用SpringMVC提供rediret:关键字进行重定向页面跳转
语法:return “redirect:/index.jsp”
注意:使用redirect跳转页面不会经过视图解析器
2.Controller跳转到Controller
forward跳转到Controller:
使用SpringMvc提供的关键字forward
语法:forward:/跳转类上@requestMapping的值/跳转类中方法@RequestMapping的值
例子:return “forward:test:/”;
redirect跳转到Controller:使用SpringMvc提供的关键字redirect
语法:redirect:/跳转类上@requestMapping的值/跳转类中方法@RequestMapping的值
forward
跳转到页面(.jsp
)示意图:
redirect
跳转到页面(.jsp
)示意图:
forward方法:
Controller层跳转到另外一个控制层(controller
)示意图:
- 如果跳本类的一个控制器(
controller
)方法示意图:
redirect方法
: Controller层跳转到另外一个控制层(controller
)示意图:
2、跳转方法总结
===================================================================================
接收参数语法说明:springmvc中使用控制器方法参数来收集客户端的请求参数,因此在接收请求参数时直接在需要控制器方法声明即可,springmvc可以自动根据指定类型完成类型的转换操作
1、接收零散类型的参数
如:八种基本类型+String+日期类型
Controller
中的业务方法的参数名称要与请求参数的name一致,参数值会自动映射匹配。并且能自动做类型
转换;
- 自动的类型转换是指从String向其他类型 的转换
(1)前端传递参数
#get方式传递参数
http://localhost:8080/SpringMvc/user/register?
name=王恒杰&password=234&sex=true&birthday=1999/12/03&salary=20000
#post方式传递参数
姓名:
密码:
性别:
生日:
工资:
(2)后台Controller接收数据
//负责控制层组件对象创建
@Controller(“userController”)
//类上的控制器映射器,相当于Struts2中的namespace
@RequestMapping(“user”)
public class UserController {
@RequestMapping(“register”)
public String register(String name, String password, Boolean sex, Date birthday, Double salary) {
System.out.println(“注册成功”);
System.out.println(“姓名” + name);
System.out.println(“密码” + password);
System.out.println(“性别” + sex);
System.out.println(“生日” + birthday);
System.out.println(“工资” + salary);
return “index”;
}
注意:springmvc在接收日期类型参数时日期格式必须为yyyy/MM/dd HH:mm:ss
2、接收对象类型参数
(1)前端传递参数
姓名:
密码:
性别:
生日:
工资:
- 注意:
在接收对象类型参数时和struts2接收不同,springmvc直接根据传递参数名与对象中属性名一致自动封装对象
(2)后台Controller接收数据
- 实体类
public class User implements Serializable {
private static final Long serialVersionUID=1L;
private String name;
private String password;
private String sex;
private String birthday;
private String Salary;
- 控制器中接收前端数据
//负责控制层组件对象创建
@Controller(“userController”)
//类上的控制器映射器,相当于Struts2中的namespace
@RequestMapping(“user”)
public class UserController{
@RequestMapping(“register”)
public String register(User user){
System.out.println(“注册成功”);
System.out.println(“姓名”+user.getName());
System.out.println(“密码”+user.getPassword());
System.out.println(“性别”+user.getSex());
System.out.println(“生日”+user.getBirthday());
System.out.println(“工资”+user.getSalary());
return “redirect:/index.jsp”;
}
3、接收数组类型参数
(1)前端传递参数
(2)后台Controller接收数据
//负责控制层组件对象创建
@Controller(“userController”)
//类上的控制器映射器,相当于Struts2中的namespace
@RequestMapping(“user”)
public class UserController{
@RequestMapping(“register”)
public String register(User user, String[] hobby){
System.out.println(“注册成功”);
System.out.println(“姓名:”+user.getName());
System.out.println(“密码:”+user.getPassword());
System.out.println(“性别:”+user.getSex());
System.out.println(“生日:”+user.getBirthday());
System.out.println(“工资:”+user.getSalary());
System.out.println(“爱好:”);
for (String s : hobby) {
System.out.println(s);
}
return “redirect:/index.jsp”;
}
4、SpringMVC接收参数中文乱码解决方案
(1)在使用post方式接收请求参数时出现中文乱码接收数据问题解决
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
charset
/*
(2)在使用set方式中文乱码问题
改tomcat的server.xml
配置文件大概在50多行加上URIEncoding="UTF-8"
1.针对于GET方式中文乱码解决方案:
<Connector connectionTimeout=“20000”
port=“8080”
protocol=“HTTP/1.1”
redirectPort=“8443” URIEncoding=“UTF-8”/>
- 成功设置之后,接收到的数据就不会乱码了!
5、接收集合类型参数
说明:springmvc不支持直接将接收集合声明为控制器方法参数进行接收,
如果要接收集合类型参数必须使用对象封装要接收接收类型才可以
(1)前端传递参数
姓名:
密码:
性别:
生日:
工资:
爱好:
吃饭:
睡觉:
爱杨福君:
抽烟:
喝酒:
烫头发:
擅长技术:
python
java
c++
linux
计算机网络
go
(2)后台Controller接口
// 1.封装接收集合类型对象---->在spring mvc中用来接收集合类型参数
class Collection{
private List lists;
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
}
// 2.控制器中接收集合类型参数
//负责控制层组件对象创建
@Controller(“userController”)
//类上的控制器映射器,相当于Struts2中的namespace
@RequestMapping(“user”)
public class UserController{
总结
在这里,由于面试中MySQL问的比较多,因此也就在此以MySQL为例为大家总结分享。但是你要学习的往往不止这一点,还有一些主流框架的使用,Spring源码的学习,Mybatis源码的学习等等都是需要掌握的,我也把这些知识点都整理起来了
go
(2)后台Controller接口
// 1.封装接收集合类型对象---->在spring mvc中用来接收集合类型参数
class Collection{
private List lists;
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
}
// 2.控制器中接收集合类型参数
//负责控制层组件对象创建
@Controller(“userController”)
//类上的控制器映射器,相当于Struts2中的namespace
@RequestMapping(“user”)
public class UserController{
总结
在这里,由于面试中MySQL问的比较多,因此也就在此以MySQL为例为大家总结分享。但是你要学习的往往不止这一点,还有一些主流框架的使用,Spring源码的学习,Mybatis源码的学习等等都是需要掌握的,我也把这些知识点都整理起来了
[外链图片转存中…(img-pLerUCCX-1714964403536)]
[外链图片转存中…(img-bOeP87QE-1714964403536)]