SpringMVC基础知识汇总

今天有个非常重要的事耽搁了一会,但这并不影响我继续更新博文,其实换种思考方式,有的时候根本不是人去养成好习惯,而是一些好习惯在养成人.这是不争的事实,一个好的习惯,不仅可以提升一个人自我思考能力,还可以提升个人对事情的见解,你的见解比别人深,你做起事来才会比别人快.

那么今天来总结分享一下,SpringMVC的基础知识,我个人见解并不是很深,我会尽我所能将我学过的知识一一汇聚出来,感谢各位理解和认可.


1.SpringMVC中的重要组件

1:dispatcherServlet:

前端控制器,接受所有的请求.

2:HandlerMapping:

解析请求格式的,判断希望要执行哪个具体方法.

3:HandlerAdapter:

负责调用具体方法

4:ViewResovler:

视图解析器,解析结果,准备跳转到具体的物理视图.

2.SpringMVC运行原理图

3.SpringMVC环境搭建

1:导入所需jar包

2:在web.xml文件中配置前端控制器DispacterServlet

<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">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置SpringMVC的配置文件,存放的位置,如果不设置,位置默认在/WEB-INF/-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

3:在src下创建springmvc.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:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
       <!--扫描注解-->
   <context:component-scan base-package="com.xh.controller"></context:component-scan>
</beans>

4:编写控制类

@Controller//表示该类为控制器类
public class DemoController {
    //设置该方法的请求地址
    @RequestMapping("demo")
    public String demo(){
        System.out.println("执行了Demo方法");
        return "main.jsp";
    }
}

4.传参

1:把内容写到方法参数中

2:基本数据类型传参,默认保证参数名称和请求中传递的参数名相同

@RequestMapping("demo2")
public String demo2(String username,String password){
    System.out.println(username+"==="+password);
    return "main.jsp";
}

3:如果请求参数和方法参数名不对应使用@RequestParam()赋值

@RequestMapping("demo2")
public String demo2(String username,@RequestParam(value = "pwd") String password){
    System.out.println(username+"==="+password);
    return "main.jsp";
}

4:如果请求参数中包含中文则会出现乱码

在web.xml中配置filter--字符编码过滤器

<!--字符编码过滤器-->
<filter>
    <filter-name>encoding</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>encoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

5:给参数设置默认值

如果你的方法参数是基本数据类型(不是封装类)可以通过:@RequestParam()设置默认值

6:参数是对象类型

请求参数名和对象中属性名对应

 @RequestMapping("demo4")
public String demo4(Student stu){
    System.out.println(stu);
    return "main.jsp";
}

5.Restful传值

1:简化jsp中参数编码格式

2:在jsp中设置特定的格式

<a href="demo3/admin/18">传值方式restful</a>

3:在控制器类中

        在@RequestMapping中一定要有请求格式对应的

        {名称}中的名称可以自定义

        @PathVarible获取@RequestMapping中内容,默认按照方法的参数名去寻找

 @RequestMapping("demo3/{name1}/{age1}")
    public String demo3(@PathVariable("name1") String name, @PathVariable("age1") int age){
        System.out.println(name+"  "+age);
        //"/"表示根目录
        return "/main.jsp";
    }

6.跳转方式

1:默认跳转方式是请求转发

2:设置返回值字符串内容

        添加forward:资源途径,或者省略forward:转发

        添加redirect:资源路径,重定向

7.视图解析器

1:SpringMVC会默认提供视图解析器

2:程序员自定义视图解析器

<!--设置视图解析器-->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!--前缀-->
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

3:如果希望不执行自定义的解析器,在方法返回值前面添加forward或redirect

8.文件上传

1:基于apache的commons-fileupload.jar完成的文件上传

2:MultipartResolver的作用:

        把客户端上传的文件流转换成MultipartFile封装类

        通过MultipartFile封装类获取文件流

3:表单数据类型分类

        在<form>的enctype属性控制表单类型

        默认值application/x-www-form-urlencoded,普通表单数据(少量的文字信息)

        text/plain大量文字时使用的类型,邮件,论文

        multipart/form-data表单中包含二进制文件内容

4:编写jsp页面

 <form action="" method="post" enctype="multipart/form-data">
      <input type="file" name="file"><br/>
      <input type="submit">
    </form>

5:配置springmvc.xml

<!--MultipartResolver解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
   <property name="maxUploadSize" value="50000"></property>
</bean>
<!--异常解析器-->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
   <property name="exceptionMappings">
      <props>
         <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">/error.jsp</prop>
      </props>
   </property>
</bean>

6:编写controller控制器类

@Controller
public class uploadController{
    @RequestMapping("upload")
    public  String upload(){
        //获取文件名
        String filename=file.getOriginalFilename();
        //文件上传
        FileUtils.copyInputStreamToFile(file.getInputStream(),new File("E:/"+filename));        
        return "index.jsp";
    }
}

9.文件下载

1:访问资源时响应头如果没有设置Content-Disopsition,浏览器默认按照inline值进行处理

        inline:能显示就显示,不能显示就下载

2:只需要修改响应头中的Context-Disposition="attachment;filename=文件名"

        attachment下载,以附件形式下载

        filename=值就是要下载的显示文件名

<a href="download?filename=a.txt">下载</a>

3:编写Controller

 @RequestMapping("download")
    public void download(String filename, HttpServletResponse resp, HttpServletRequest req)throws IOException{
        //设置响应流中的文件
        resp.setHeader("Content-Disposition","attachment;filename="+filename);
        //把二进制流放入到响应体中
        ServletOutputStream os = resp.getOutputStream();
        //设置文件的地址
        String path = req.getServletContext().getRealPath("file");
        File file = new File(path,filename);
        byte[] bytes = FileUtils.readFileToByteArray(file);
        os.write(bytes);os.flush();
        os.close();

    }

到此,SpringMVC基础知识点,就跟大家分享完毕,如果有哪里不足或者有更好的建议欢迎留言吐槽,如果有哪里不懂可以私信博主,博主将一一解答,感谢认可.我们明天见! 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

道而起

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值