在用SSM做項目的時候遇到的一些小坑

1.靜態資源的配置


如圖,靜態文件.html放在webapp->WEB-INF->views下
那麼利用應該怎麼配置呢
一般給出的配置方式是這樣的

<mvc:resources location="/" mapping="/**"></mvc:resources>
  <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
  <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
  <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>

其中location是靜態文件在本地的地址,mapping是需要映射到瀏覽器url的地址,也就是當我們請求127.0.0.1:8080/js/xxx.js時,服務器會在location指定的本地目錄下尋找這個靜態文件。
然而當我直接拷過來還是找不到,顯然是因爲我的本地路徑不對。
先看在ecilpse中文件結構目錄

WebContent下直接放的靜態文件,那麼location的其實位置是什麼呢,看一下IDEA的 project structure

也就是說/是webapp這個路徑
所以正確的配置應該是

<!--config the static file
        location means filepath in project ,mapping means the url mapped
        'webapp' is the start path of location , which is '/'
    -->
    <mvc:resources location="/WEB-INF/views/" mapping="/*.html"/>
    <mvc:resources location="/WEB-INF/views/js/" mapping="/js/**"/>
    <mvc:resources location="/WEB-INF/views/css/" mapping="/css/**"/>
    <mvc:resources location="/WEB-INF/views/images/" mapping="/images/**"/>

2.配置mvc:resources後項目報404的問題

剛開始沒有配置mvc:resources,controller能夠正確訪問,但是由於web.xml使用/攔截了所有的請求,所以靜態資源訪問不上
增加mvc:resources之後,靜態資源是能訪問上了,但是註解配置的controller卻又找不到了

原因是少了 的配置,在沒有配置mvc:resources的時候沒有問題,一旦配置了mvc:resources,註解方式的url就沒有加載

補上 就可以解決問題

<!--if you use annotation you must configure following setting
        if not, when you config the static file use 'mvc:resource',
        the url define by annotation will not in operation
    -->
    <mvc:annotation-driven/>

參考:https://blog.csdn.net/liujunyu_10/article/details/77860369

3.@ResponseBody註解

@responseBody註解的作用是將controller的方法返回的對象通過適當的轉換器轉換爲指定的格式之後,寫入到response對象的body區,通常用來返回JSON數據或者是XML數據,需要注意的呢,在使用此註解之後不會再走試圖處理器,而是直接將數據寫入到輸入流中,他的效果等同於通過response對象輸出指定格式的數據。

@RequestMapping("/login")
  @ResponseBody
  public User login(User user){
    return user;
  }

User字段:userName pwd
那麼在前臺接收到的數據爲:’{“userName”:”xxx”,”pwd”:”xxx”}’

效果等同於如下代碼:

@RequestMapping("/login")
  public void login(User user, HttpServletResponse response){
    response.getWriter.write(JSONObject.fromObject(user).toString());
  }

4.Ajax請求含有@ResponseBody註解的請求報500


報錯代碼如下

@RequestMapping(value = "/login", method = RequestMethod.POST)
    public @ResponseBody User user(@RequestParam("username") String username, @RequestParam("password") String password){
        System.out.println("post");
        User user = userService.findByName(username);
        System.out.println(user);
        return user;
    }

少了json的包
加入

<!--json 版本號-->
    <jackson.version>2.8.7</jackson.version>
<!--json-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>${jackson.version}</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>${jackson.version}</version>
    </dependency>

即可正常請求

發佈了24 篇原創文章 · 獲贊 8 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章