1. 程式人生 > >SpringMVC之Controller簡單使用

SpringMVC之Controller簡單使用

//環境 spring-4.3.18/JDK1.8/開發工具/IntelliJ IDEA 2018.2.5 x64

//工程結構圖

//web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
5 version="4.0"> 6 <!--配置Spring IoC配置檔案路徑--> 7 <context-param> 8 <param-name>contextConfigLocation</param-name> 9 <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/dispatcher-servlet.xml</param-value> 10 </context-param>
11 <!--初始化Spring IoC容器--> 12 <listener> 13 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 14 </listener> 15 <!--Spring MVC會根據servlet-name配置,找到/WEB-INF/dispatcher-servlet.xml作為配置檔案載入web工程--> 16 <servlet
> 17 <servlet-name>dispatcher</servlet-name> 18 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 19 <!--配置Spring IoC配置檔案路徑--> 20 <!--<init-param> 21 <param-name>contextConfigLocation</param-name> 22 <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/dispatcher-servlet.xml</param-value> 23 </init-param>--> 24 <load-on-startup>1</load-on-startup> 25 </servlet> 26 <!--Controller-Servlet攔截配置--> 27 <servlet-mapping> 28 <servlet-name>dispatcher</servlet-name> 29 <url-pattern>/</url-pattern> 30 </servlet-mapping> 31 </web-app>

//applicationContext.xml    該檔案可省略,同時將

<!--配置Spring IoC配置檔案路徑-->
 7     <context-param>
 8         <param-name>contextConfigLocation</param-name>
 9         <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/dispatcher-servlet.xml</param-value>
10     </context-param>

改成
<!--配置Spring IoC配置檔案路徑-->
 7     <context-param>
 8         <param-name>contextConfigLocation</param-name>
 9         <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
10     </context-param>
 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:mvc="http://www.springframework.org/schema/mvc"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xmlns:aop="http://www.springframework.org/schema/aop"
 7        xmlns:tx="http://www.springframework.org/schema/tx"
 8        xsi:schemaLocation="http://www.springframework.org/schema/beans
 9         http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
10         http://www.springframework.org/schema/mvc
11         http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
12         http://www.springframework.org/schema/context
13         http://www.springframework.org/schema/context/spring-context.xsd
14         http://www.springframework.org/schema/aop
15         http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
16         http://www.springframework.org/schema/tx
17         http://www.springframework.org/schema/tx/spring-tx.xsd">
18 
19 
20 
21 
22 </beans>

//dispatcher-servlet.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:mvc="http://www.springframework.org/schema/mvc"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xmlns:aop="http://www.springframework.org/schema/aop"
 7        xmlns:tx="http://www.springframework.org/schema/tx"
 8        xsi:schemaLocation="http://www.springframework.org/schema/beans
 9         http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
10         http://www.springframework.org/schema/mvc
11         http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
12         http://www.springframework.org/schema/context
13         http://www.springframework.org/schema/context/spring-context.xsd
14         http://www.springframework.org/schema/aop
15         http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
16         http://www.springframework.org/schema/tx
17         http://www.springframework.org/schema/tx/spring-tx.xsd">
18 
19     <!--使用註解驅動-->
20     <mvc:annotation-driven></mvc:annotation-driven>
21     <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
22     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>-->
23     <!--掃描裝載的包-->
24     <context:component-scan base-package="com.jhc.controller"></context:component-scan>
25     <!--定義檢視解析器-->
26     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
27 
28 </beans>

//index.jsp

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: JHC
 4   Date: 2018/11/30
 5   Time: 17:31
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10   <head>
11     <title>Hello Word</title>
12   </head>
13   <body>
14   Say:${hello}<br>
15   </body>
16 </html>

//HelloController.java

 1 package com.jhc.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.servlet.ModelAndView;
 6 
 7 @Controller
 8 public class HelloController {
 9 
10     @RequestMapping(value = "/hello")
11     public ModelAndView sayHello(){
12         System.out.println("Hello Word");
13         ModelAndView modelAndView=new ModelAndView();
14         modelAndView.addObject("hello","Hello Word");
15         modelAndView.setViewName("/index.jsp");
16         return modelAndView;
17     }
18 }