标签:ring conf 映射 bean 接收 json 位置 ... handle
原教程: https://www.w3cschool.cn/wkspring/
模型封装了应用程序数据,并且通常它们由 POJO 组成。
视图主要用于呈现模型数据,并且通常它生成客户端的浏览器可以解释的 HTML 输出。
spring MVC是围绕DispatcherServlet 设计的,DispatcherServlet 用来处理所有的 HTTP 请求和响应。
DispatcherServlet 接收到HTTP请求后:
控制器接受请求,并基于使用的 GET 或 POST 方法来调用适当的 service 方法。并返回视图名称到 DispatcherServlet 中。
DispatcherServlet 会从 ViewResolver 为请求检取定义视图。
1 // WebContent/WEB-INF/web.xml
<web-app id="WebApp_ID" version="2.4" 2 xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 6 <display-name>Spring MVC Application</display-name> 7 <servlet> 8 <servlet-name>HelloWeb</servlet-name> 9 <servlet-class> 10 org.springframework.web.servlet.DispatcherServlet 11 </servlet-class> 12 <load-on-startup>1</load-on-startup> 13 </servlet> 14 <servlet-mapping> 15 <servlet-name>HelloWeb</servlet-name> 16 <url-pattern>*.jsp</url-pattern> 17 </servlet-mapping> 18 </web-app>
1 // web.xml 2 <web-app...> 3 4 .... 5 <context-param> 6 <param-name>contextConfigLocation</param-name> 7 <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value> 8 </context-param> 9 <listener> 10 <listener-class> 11 org.springframework.web.context.ContextLoaderListener 12 </listener-class> 13 </listener> 14 </web-app> 15 16 // WebContent/WEB-INF/HelloWeb-servlet.xml 17 <beans xmlns="http://www.springframework.org/schema/beans" 18 xmlns:context="http://www.springframework.org/schema/context" 19 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 20 xsi:schemaLocation=" 21 http://www.springframework.org/schema/beans 22 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 23 http://www.springframework.org/schema/context 24 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 25 26 <context:component-scan base-package="com.tutorialspoint" /> 27 28 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 29 <property name="prefix" value="/WEB-INF/jsp/" /> 30 <property name="suffix" value=".jsp" /> 31 </bean> 32 33 </beans>
<context:component-scan>用于激活spring MVC注释扫描功能,如@Controller和@RequestMapping等
InternalResourceViewResolver定义了检索视图的规则。一个名称为 hello 的逻辑视图将发送给位于 /WEB-INF/jsp/hello.jsp
示例1:
1 @Controller 2 @RequestMapping("/hello") 3 public class HelloController{ 4 @RequestMapping(method = RequestMethod.GET) 5 public String printHello(ModelMap model) { 6 model.addAttribute("message", "Hello Spring MVC Framework!"); 7 return "hello"; 8 } 9 }
示例2:
1 @Controller 2 public class HelloController{ 3 @RequestMapping(value = "/hello", method = RequestMethod.GET) 4 public String printHello(ModelMap model) { 5 model.addAttribute("message", "Hello Spring MVC Framework!"); 6 return "hello"; 7 } 8 }
spring mvc支持的视图有很多,比如JSP、HTML、PDF、Excel 工作表、XML、Velocity 模板、XSLT、JSON、Atom 和 RSS 提要、JasperReports 等等
1 // hello.jsp 2 <html> 3 <head> 4 <title>Hello Spring MVC</title> 5 </head> 6 <body> 7 <h2>${message}</h2> 8 </body> 9 </html>
注意表单文件中action的路径问题,一般是servlet-name/service方法名
每个service方法都与一个网页页面关联
1 @Controller 2 public class WebController { 3 @RequestMapping(value = "/index", method = RequestMethod.GET) 4 public String index() { 5 return "index"; 6 } 7 @RequestMapping(value = "/redirect", method = RequestMethod.GET) 8 public String redirect() { 9 return "redirect:finalPage"; 10 } 11 @RequestMapping(value = "/finalPage", method = RequestMethod.GET) 12 public String finalPage() { 13 return "final"; 14 } 15 }
标签:ring conf 映射 bean 接收 json 位置 ... handle
原文地址:https://www.cnblogs.com/yfs123456/p/10669671.html