springmvc知识点

1.

ServletContext

对于一个web应用,其部署在web容器中,web容器提供其一个全局的上下文环境,这个上下文就是我们的ServletContext,其为后面的spring IoC容器提供一个宿主环境

spring上下文,springmvc上下文

springMVC容器只负责创建Controller对象,不会创建service和dao,并且他是一个子容器。而spring的容器只负责Service和dao对象,是一个父容器。子容器可以看见父容器的对象,而父容器看不见子容器的对象,这样各司其职。 

2.url匹配优先级

不论有多少个/*/*,都比/**优先级高.

当有多个*和多个‘{}'时,命中单个路径多的,优先越高。

前缀匹配优先级低,比非前缀的低

*,{}越少优先级越高

路径越短优先级越高

3.form表单出现中文乱码,在web.xml文件添加如下过滤器

<!--解决乱码的过滤器-->
    <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-mapping呢,是让所有的请求都过一下这个filter,即让过滤器过滤一下子-->
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

4.在idea中测试http响应可以在test文件夹下新建一个user.http(只要以.http结尾就行),格式和帮助文档在建立该文件后,右上角会提示,具体文档全文如下

### Send POST request with json body
POST https://httpbin.org/post
Content-Type: application/json

{
  "id": 999,
  "value": "content"
}

### Send POST request with body as parameters
POST https://httpbin.org/post
Content-Type: application/x-www-form-urlencoded

id=999&value=content

### Send a form with the text and file fields
POST https://httpbin.org/post
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="element-name"
Content-Type: text/plain

Name
--WebAppBoundary
Content-Disposition: form-data; name="data"; filename="data.json"
Content-Type: application/json

< ./request-form-data.json
--WebAppBoundary--

### Send request with dynamic variables in request's body
POST https://httpbin.org/post
Content-Type: application/json

{
  "id": {{$uuid}},
  "price": {{$randomInt}},
  "ts": {{$timestamp}},
  "value": "content"
}

###

5.当需要传递复杂参数的时候,需要建立一个vo类,专门用于传参

@Data
public class QueryVo {
    private String sortField;
    private User user;
    private Long[] ids;
    private List<User> userList;
    private Map<String, User> userMap;
}

6.entity对象的属性应该保持和数据库中的字段名完全一致.

7.越靠近po,复用度越低

8.

9.避免每次添加新的包都手动引入的办法: project settings->artifacts->左上加号->WEBapplication EXPLORED ->第二个选项(from module) 然后重新部署tomcat, configuration->Deployment  然后reload下maven

10.thymeleaf  乱码,添加第二行

<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
        <property name="characterEncoding" value="utf-8" />
        <property name="order" value="1"></property>
</bean>
<bean id="templateResolver"
          class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
        <property name="prefix" value="/WEB-INF/templates/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML" />
        <property name="cacheable" value="true" />
        <property name="characterEncoding" value="utf-8" />
</bean>

11. 上传文件时,要在springmvc的配置文件中添加如下内容(application.xml)

<!--    文件上传bean-->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--上传文件的最大大小,单位为字节 -->
        <property name="maxUploadSize" value="17367648787"></property>

        <!-- 上传文件的编码 -->
        <property name="defaultEncoding" value="UTF-8"></property>
</bean>

同时要注意表单的名字和controller中的

@RequestParam("textFile") 的参数要一致 
public R upload(@RequestParam("textFile") CommonsMultipartFile file){
<form action="/application/upload" enctype="multipart/form-data" method="post">
    <input type="file" class="file" name="textFile"/>
    <input type="submit" />
</form>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值