SpringMVC基础-14-SpringMVC与Spring整合
代码示例:
BookController.java:
1 package com.atguigu.controller; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 7 import com.atguigu.service.BookService; 8 9 @Controller 10 public class BookController { 11 12 @Autowired 13 private BookService bookService; 14 15 public BookController(){ 16 System.out.println("BookController..."); 17 } 18 19 @RequestMapping("/hello") 20 public String hello(){ 21 System.out.println(bookService); 22 return "forward:/index.jsp"; 23 } 24 25 }
HahaController.java:
1 package com.atguigu.controller; 2 3 import org.springframework.stereotype.Controller; 4 5 @Controller 6 public class HahaController { 7 8 }
BookService.java:
1 package com.atguigu.service; 2 3 import org.springframework.stereotype.Service; 4 5 @Service 6 public class BookService { 7 8 public BookService(){ 9 System.out.println("BookService...."); 10 } 11 12 }
HahaService.java:
1 package com.atguigu.service; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Service; 5 6 import com.atguigu.controller.HahaController; 7 8 @Service 9 public class HahaService { 10 11 //@Autowired 12 //HahaController hahaController; 13 14 }
spring.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 7 <context:component-scan base-package="com.atguigu"> 8 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 9 <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> 10 </context:component-scan> 11 12 </beans>
springmvc.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 9 10 <context:component-scan base-package="com.atguigu" use-default-filters="false"> 11 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 12 <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> 13 </context:component-scan> 14 15 </beans>
web.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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_2_5.xsd" id="WebApp_ID" version="2.5"> 3 <display-name>7.SpringMVC_crud</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <!-- needed for ContextLoaderListener --> 14 <context-param> 15 <param-name>contextConfigLocation</param-name> 16 <param-value>classpath:spring.xml</param-value> 17 </context-param> 18 19 <!-- Bootstraps the root web application context before servlet initialization --> 20 <listener> 21 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 22 </listener> 23 24 <servlet> 25 <servlet-name>springDispatcherServlet</servlet-name> 26 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 27 <init-param> 28 <param-name>contextConfigLocation</param-name> 29 <param-value>classpath:springmvc.xml</param-value> 30 </init-param> 31 <load-on-startup>1</load-on-startup> 32 </servlet> 33 <servlet-mapping> 34 <servlet-name>springDispatcherServlet</servlet-name> 35 <url-pattern>/</url-pattern> 36 </servlet-mapping> 37 <filter> 38 <filter-name>CharacterEncodingFilter</filter-name> 39 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 40 <init-param> 41 <param-name>encoding</param-name> 42 <param-value>utf-8</param-value> 43 </init-param> 44 <init-param> 45 <param-name>forceEncoding</param-name> 46 <param-value>true</param-value> 47 </init-param> 48 </filter> 49 <filter-mapping> 50 <filter-name>CharacterEncodingFilter</filter-name> 51 <url-pattern>/*</url-pattern> 52 </filter-mapping> 53 <filter> 54 <filter-name>HiddenHttpMethodFilter</filter-name> 55 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 56 </filter> 57 <filter-mapping> 58 <filter-name>HiddenHttpMethodFilter</filter-name> 59 <url-pattern>/*</url-pattern> 60 </filter-mapping> 61 </web-app>
myerror.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h1>出错啦!</h1> 11 <h2>错误信息:${ex }-${exception }</h2> 12 </body> 13 </html>
success.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 System.out.println("success.jsp...."); 12 %> 13 <h1>成功!</h1> 14 </body> 15 </html>
index.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 <% 9 pageContext.setAttribute("ctp", request.getContextPath()); 10 %> 11 </head> 12 <body> 13 14 <a href="${ctp }/handle01?i=10">test01-哈哈</a><br/> 15 <a href="${ctp }/handle02?username=admin">handle02</a><br/> 16 <a href="${ctp }/handle03">handle03</a><br/> 17 <a href="${ctp }/handle04">handle04</a> 18 </body> 19 </html>