SpringMVC:html
SpringMVC是Spring爲展示層提供的基於MVC設計的優秀的Web框架,是目前最主流的MVC框架之一java
SpringMVC經過註解,讓POJO成爲處理請求的控制器,而無須實現任何的接口同時SprinMVC支持web
Rest風格的URL請求,採用了鬆散耦合可插拔組件結構,比其餘MVC框架更具備擴展性和靈活性spring
入門案例:HelloWorldspring-mvc
步驟:mvc
1 加入相應JAR包(若是是Maven則不須要此步驟)app
2 在Web.XML中配置DispatcherServlet框架
3 加入SpringMVC的配置文件jsp
4 編寫處理請求的處理器,而且標識爲處理器(controller)async
5 編寫視圖
細化步驟:
加入jar包以下圖
配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.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:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 此處能夠能夠配置成*.do --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
編寫請求處理器
package com.cn.hnust.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; //使用RequestMapping註解來請求URl配合Spring-mvc.xml文件中的視圖解析器獲得相應的頁面 @Controller public class Helloworld { @RequestMapping("/helloworld") public String hello(){ system.out.pringtln(hello world); return "success"; } }
編寫視圖(index.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a herf="helloworld">Hello</a>
</body>
</html>
編寫成功頁面(success.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> Success </body> </html>
Spring-mvc.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:p="http://www.springframework.org/schema/p" 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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- <mvc:annotation-driven /> 會自動註冊DefaultAnnotationHandlerMapping與AnnotationMethodHa ndlerAdapter 兩個bean,是spring MVC爲@Controllers分發請求所必須的。 --> <mvc:annotation-driven /> <!-- 自動掃描該包,使SpringMVC認爲包下用了@controller註解的類是控制器 --> <context:component-scan base-package="com.cn.hnust.controller" /> <!-- 啓動SpringMVC的註解功能,完成請求和註解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON轉換器 --> </list> </property> </bean> <!-- 定義跳轉的文件的先後綴 ,視圖模式配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 這裏的配置個人理解是自動給後面action的方法return的字符串加上前綴和後綴,變成一個 可用的url地址 --> <property name="prefix" value="/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
運行測試結果: