??wei_shuo的個(gè)人主頁(yè)
??wei_shuo的學(xué)習(xí)社區(qū)
??Hello World !
Controller配置
控制器Controller
- 控制器復(fù)雜提供訪問(wèn)應(yīng)用程序的行為,通常通過(guò)
接口定義
或注釋定義
的兩種方法實(shí)現(xiàn)- 控制器負(fù)責(zé)解析用戶的請(qǐng)求并將其轉(zhuǎn)換為一個(gè)模型
- SpringMVC中一個(gè)控制器可以包含多個(gè)方法
實(shí)現(xiàn)Controller接口
Controller是一個(gè)接口,在org.springframework.web.servlet.mvc包下,接口只有一個(gè)方法
//實(shí)現(xiàn)Controller接口的類可以獲得控制器的功能
public interface Controller {
//處理請(qǐng)求且返回一個(gè)模型與視圖對(duì)象
@Nullable
ModelAndView handleRequest(HttpServletRequest var1, HttpServletResponse var2) throws Exception;
}
接口方法實(shí)現(xiàn)Controller
- 配置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"> <!--配置DispatcherServlet--> <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-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
- 配置springmvc-servlet.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--自動(dòng)掃描包,讓指定包下的注釋生效,由IOC容器統(tǒng)一管理--> <context:component-scan base-package="com.wei.controller"/> <!--過(guò)濾靜態(tài)資源--> <mvc:default-servlet-handler/> <!--配置annotation-driven使:處理器映射器 和 處理器適配器 自動(dòng)完成實(shí)例的注入--> <mvc:annotation-driven/> <!--視圖解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <!--前綴--> <property name="prefix" value="/WEB-INF/jsp/"/> <!--后綴--> <property name="suffix" value=".jsp"/> </bean> </beans>
- 編寫(xiě)Controller類(ControllerTest1)
package com.wei.controller; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //實(shí)現(xiàn)了Controller類說(shuō)明這就是控制器 public class ControllerTest1 implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { //創(chuàng)建模型和視圖對(duì)象 ModelAndView modelAndView = new ModelAndView(); 封裝數(shù)據(jù),向模型添加屬性msg與值,可以在jsp頁(yè)面取出、渲染 modelAndView.addObject("msg","ControllerTest1"); //視圖跳轉(zhuǎn) modelAndView.setViewName("test"); //返回視圖模型對(duì)象 return modelAndView; } }
- test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${msg} </body> </html>
缺點(diǎn):一個(gè)控制器只要一個(gè)方法,如果多個(gè)方法需要多個(gè)Controller,使用這種方法比較麻煩
注解方法實(shí)現(xiàn)Controller
@Controller注解的類會(huì)自動(dòng)添加到Spring上下文中,被這個(gè)注解的類,返回String,并且有具體頁(yè)面跳轉(zhuǎn),就會(huì)被視圖解析器解析
@Controller控制器(注入服務(wù))
@Component(把普通pojo實(shí)例化到spring容器中,相當(dāng)于配置文件中的 )
@Service服務(wù)(注入dao)
@Respository(現(xiàn)dao訪問(wèn))
- Spring可以使用掃描機(jī)制找到應(yīng)用程序中所有基于注解的控制器類,為保證Spring能找到控制器,需要在配置文件中聲明組件掃描
<!--自動(dòng)掃描包,讓指定包下的注釋生效,由IOC容器統(tǒng)一管理--> <context:component-scan base-package="com.wei.controller"/>
- 增加一個(gè)ControllerTest類,使用注解實(shí)現(xiàn)
package com.wei.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; //@Controller注解的類會(huì)自動(dòng)添加到Spring上下文中,被這個(gè)注解的類,返回String,并且有具體頁(yè)面跳轉(zhuǎn),就會(huì)被視圖解析器解析 @Controller public class ControllerTest2 { @RequestMapping("/t2") public String test1(Model model){ model.addAttribute("msg","ControllerTest2"); return "test"; // /WEB-INF/jsp/test.jsp } }
RequestMapping配置
- @RequestMapping注解用于映射url到控制器或一個(gè)特定的處理程序方法,可用于類或方法上
- 用于類上,表示類中的所有響應(yīng)請(qǐng)求的方法都是以該地址作為父路徑
- 訪問(wèn)路徑為:http://localhost:8080/項(xiàng)目名/c3/t1
package com.wei.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/c3") public class ControllerTest3 { @RequestMapping({"/t1"}) //或者 @RequestMapping({"/c3/t1"}) public String test1(Model model){ model.addAttribute("msg","ControllerTest3"); return "test"; } }
RestFul
概念
RestFul是一種網(wǎng)絡(luò)應(yīng)用程序的設(shè)計(jì)風(fēng)格和開(kāi)發(fā)方式,基于
HTTP
,可以使用XML
格式定義或JSON
格式定義。RestFul適用于移動(dòng)互聯(lián)網(wǎng)廠商作為業(yè)務(wù)接口的場(chǎng)景,實(shí)現(xiàn)第三方OTT
調(diào)用移動(dòng)網(wǎng)絡(luò)資源的功能,動(dòng)作類型為新增、變更、刪除所調(diào)用資源RestFul就是資源定位及資源操作的風(fēng)格,不是標(biāo)準(zhǔn)也不是協(xié)議,只是一種風(fēng)格,基于RestFul風(fēng)格設(shè)計(jì)的軟件可以更高效、有層次、易于實(shí)現(xiàn)緩存機(jī)制
傳統(tǒng)方式
package com.wei.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
//@Controller注解的類會(huì)自動(dòng)添加到Spring上下文中,被這個(gè)注解的類,返回String,并且有具體頁(yè)面跳轉(zhuǎn),就會(huì)被視圖解析器解析
@Controller
public class RestFulController {
@RequestMapping("/add")
public String test1(int a, int b, Model model) {
int result = a + b;
model.addAttribute("msg", "結(jié)果為:" + result);
return "test";
}
}
RestFul方式
@PathVariable:讓方法參數(shù)的值對(duì)應(yīng)綁定到一個(gè)URL模板變量上
package com.wei.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
//@Controller注解的類會(huì)自動(dòng)添加到Spring上下文中,被這個(gè)注解的類,返回String,并且有具體頁(yè)面跳轉(zhuǎn),就會(huì)被視圖解析器解析
@Controller
public class RestFulController {
@RequestMapping("/add/{a}/")
public String test1(@PathVariable int a,@PathVariable int b, Model model) {
int result = a + b;
model.addAttribute("msg", "結(jié)果為:" + result);
return "test";
}
}
方式一:
@GetMapping("/add/{a}/")
@PostMapping("/add/{a}/")
@PutMapping("/add/{a}/")
@DeleteMapping("/add/{a}/")
@PatchMapping("/add/{a}/")
package com.wei.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
//@Controller注解的類會(huì)自動(dòng)添加到Spring上下文中,被這個(gè)注解的類,返回String,并且有具體頁(yè)面跳轉(zhuǎn),就會(huì)被視圖解析器解析
@Controller
public class RestFulController {
// @GetMapping("/add/{a}/")
// @PostMapping("/add/{a}/")
// @PutMapping("/add/{a}/")
// @DeleteMapping("/add/{a}/")
// @PatchMapping("/add/{a}/")
@GetMapping("/add/{a}/")
public String test1(@PathVariable int a,@PathVariable int b, Model model) {
int result = a + b;
model.addAttribute("msg", "test1結(jié)果為:" + result);
return "test";
}
@PostMapping("/add/{a}/")
public String test2(@PathVariable int a,@PathVariable int b, Model model) {
int result = a + b;
model.addAttribute("msg", "test2結(jié)果為:" + result);
return "test";
}
}
方式二:
package com.wei.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
//@Controller注解的類會(huì)自動(dòng)添加到Spring上下文中,被這個(gè)注解的類,返回String,并且有具體頁(yè)面跳轉(zhuǎn),就會(huì)被視圖解析器解析
@Controller
public class RestFulController {
@RequestMapping(value = "/add/{a}/",method = RequestMethod.GET)
public String test1(@PathVariable int a,@PathVariable int b, Model model) {
int result = a + b;
model.addAttribute("msg", "test1結(jié)果為:" + result);
return "test";
}
@RequestMapping(value = "/add/{a}/",method = RequestMethod.POST)
public String test2(@PathVariable int a,@PathVariable int b, Model model) {
int result = a + b;
model.addAttribute("msg", "test2結(jié)果為:" + result);
return "test";
}
}
?? 結(jié)語(yǔ):創(chuàng)作不易,如果覺(jué)得博主的文章賞心悅目,還請(qǐng)——
點(diǎn)贊
??收藏
??評(píng)論
??文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-500707.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-500707.html
到了這里,關(guān)于SpringMVC原理分析 | Controller配置、RestFul風(fēng)格的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!