springmvc-kuang
生活随笔
收集整理的這篇文章主要介紹了
springmvc-kuang
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
流程
web.xml中配置的DispatcherServlet
配置spingmvc三大要素:處理器映射器、處理器適配器、視圖解析器
注冊控制器,id為訪問地址
注解配置springxml文件
HelloSpringmvc注解配置代碼
spring 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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:component-scan base-package="com.kuang.controller"/><!--掃描這個包下,所有注解生效--><mvc:annotation-driven/><!--配置注解驅(qū)動--><mvc:default-servlet-handler/><!--靜態(tài)資源過濾--><!--配置視圖解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"id="internalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/"/><property name="suffix" value=".jsp"/></bean></beans>web.xml
<?xml version="1.0" encoding="UTF-8"?> <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><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>測試代碼
package com.kuang.controller;import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;@org.springframework.stereotype.Controller public class Controller {@RequestMapping("/h1")public String msg(Model model){model.addAttribute("msg","HelloSpringmvcAnnotation");return "test";}}注意
遇到的問題:代碼沒問題的前提下提示404頁面
原因:tomcat本地項(xiàng)目目錄中maven依賴在發(fā)布項(xiàng)目的過程中沒有及時導(dǎo)入依賴
解決辦法:
1、IDEA中打開項(xiàng)目的project structure
2、找到artifact功能,并選中發(fā)布的項(xiàng)目
3、在WEB-INF目錄下,(與classes文件夾處于同一目錄)新建一個lib目錄
4、點(diǎn)擊上方+號,添加libiray,選中所有依賴添加
5、apply—>ok
Restful風(fēng)格
**==============================================**
Json
標(biāo)注了@RestController類下的所有方法只會返回json字符串
而@Controller會走視圖解析器,返回的字符串會用來拼接域名
遇到的問題:properties文件中屬性名前必須加jdbc.,不加就會報錯
原因
ajax技術(shù)
狀態(tài)碼
200:成功
300+:轉(zhuǎn)發(fā)/重定向
400+:客戶端錯誤
500+:服務(wù)器端錯誤
文件上傳下載
總結(jié)
以上是生活随笔為你收集整理的springmvc-kuang的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一致性哈希算法的原理与实现
- 下一篇: 思想学习——细节决定成败