Menu
Software Engineering Notes
  • About Me
Software Engineering Notes

Spring MVC

Posted on 15 Eylül 201815 Eylül 2018 by ogtadev

Spring MVC dispatcherServlet etrafında tasarlanmış bir yapıdadır. Dispatcher Servlet tüm HTTP request ve responseları handle eder.

HTTP isteği ilk geldiği zaman Dispatcher Servlet HandlerMapping’e başvurarak ilgili uygun controllerı öğrenir.

Controller gelen HTTP metotlarına göre desteklediği (POST, GET, PUT, etc.) şekilde business ve mantıksal işlemleri burada halleder. Kısacası java ile yapacağınız çoğu işlem bu katmanda gerçekleştirilebilir.

ViewResolver yardımıyla request için tanımlı view öğrenilir. Dispatcher model data olarak isimlendirilen yapı ile viewe gönderilir ve tarayıcıda render edilir.

Model data’nın örneklenmesi gerekirse view katmanına gönderilecek olan dinamik data olarak nitelendirilebilir. Örneğin bir müşteriye ait isim, soyisim ve bakiye gibi yapılar model data içine set edilerek view katmanına gönderilir.

Gerekli Konfigrasyonlar

web.xml dosyası URL mapping için kullanılır.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<web-app id = "WebApp_ID" version = "2.4"
 
   xmlns = "http://java.sun.com/xml/ns/j2ee" 
 
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
 
   xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee 
 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
   <display-name>Spring MVC Application</display-name>  
 
   <servlet>
 
      <servlet-name>HelloWeb</servlet-name>
 
      <servlet-class>
 
         org.springframework.web.servlet.DispatcherServlet
 
      </servlet-class>
 
      <load-on-startup>1</load-on-startup>
 
   </servlet>
 
   <servlet-mapping>
 
      <servlet-name>HelloWeb</servlet-name>
 
      <url-pattern>*.jsp</url-pattern>
 
   </servlet-mapping>
 
</web-app>

 

WebContent/WEB-INF altında web.xml dosyası yer almaktadır.

Dispatcher name olarak verilen HelloWeb framework tarafından uygulama contextinin yükleneceği dosyayı belirtmektedir.

[servlet-name]-servlet.xml gibi. Bu dosya WebContent/WEB-INF dizini altında ter alır. Bizim örnekte kullandığımız hali ile HelloWebservlet.xml

Bir diğer tag olan servlet-mapping tagi ile ilgili pathin hangi uzantı ile biteceğini belirler bu örnekte .jsp handle edilebilir durumdadır.

Controller Tanımlama

Bir classın Controller olması için @Controller annotation’ı kullanmak gerekir.

@RequestMapping ile Controller için bir path, ilgili ip üzerinden dışarıya açılmış olacaktır. Routing işleminde gelen pathin bu controllera ait olduğunu işaret eden annotationdır.

Örnek Kullanım:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Controller
 
public class HelloController {
 
   @RequestMapping(value = "/hello", method = RequestMethod.GET)
 
   public String printHello(ModelMap model) {
 
      model.addAttribute("message", "Hello Spring MVC Framework!");
 
      return "hello";
 
   }
 
}

Bu kullanımda value ile /hello adresine gelen(http://localhost:8999/hello) get isteğinin bu Controller’a ait metoda düşmesi sağlanır.

model.addAttribute(“message”, “Hello Spring MVC Framework!”); yapısı ile message isimli bir model data değişkenine Hello Spring MVC Framework olarak bir string atanmıştır.

JSP Views Oluşturma

Spring MVC bir sürü template engine(sunum teknolojisi) çıktısını desteklemekte. (JSP, PDF, HTML, Excel, XML, JSON, RSS etc.) Genellikle JSP ve JSF kullanılmakta.

/WEB-INF/hello/ dizini altına hello.jsp oluşturulur. İçeriği aşağıdaki şekildedir.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<html>
 
   <head>
 
      <title>Hello Spring MVC</title>
 
   </head>
 
   
 
   <body>
 
      <h2>${message}</h2>
 
   </body>
 
</html>

Bir önceki örnekte message olarak set ettiğimiz değişkeni yukarıdaki şekilde kullanabiliriz.

Static Sayfalar için Mapping işleminin HelloWeb-servlet.xml üzerinden yapılması

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<web-app id = "WebApp_ID" version = "2.4"
 
   xmlns = "http://java.sun.com/xml/ns/j2ee" 
 
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
 
   xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee 
 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
 
 
   <display-name>Spring Page Redirection</display-name>
 
 
 
   <servlet>
 
      <servlet-name>HelloWeb</servlet-name>
 
      <servlet-class>
 
         org.springframework.web.servlet.DispatcherServlet
 
      </servlet-class>
 
      <load-on-startup>1</load-on-startup>
 
   </servlet>
 
   
 
   <servlet-mapping>
 
      <servlet-name>HelloWeb</servlet-name>
 
      <url-pattern>/</url-pattern>
 
   </servlet-mapping>
 
  
 
</web-app>

HelloWeb-servlet.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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-2.5.xsd
 
   http://www.springframework.org/schema/mvc
 
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 
   http://www.springframework.org/schema/context
 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
 
 
   <context:component-scan base-package="com.tutorialspoint" />
 
     
 
   <bean id = "viewResolver" 
 
      class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
 
      
 
      <property name = "prefix" value = "/WEB-INF/jsp/" />
 
      <property name = "suffix" value = ".jsp" />
 
   </bean>
 
    
 
   <mvc:resources mapping = "/pages/**" location = "/WEB-INF/pages/" />
 
   <mvc:annotation-driven/>
 
    
 
</beans>

WEB-INF/jsp/index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
 
<html>
 
   <head>
 
      <title>Spring Landing Page</title>
 
   </head>
 
   <body>
 
      <h2>Spring Landing Pag</h2>
 
      <p>Click below button to get a simple HTML page</p>
 
      
 
      <form:form method = "GET" action = "/HelloWeb/staticPage">
 
         <table>
 
            <tr>
 
               <td>
 
                  <input type = "submit" value = "Get HTML Page"/>
 
               </td>
 
            </tr>
 
         </table>  
 
      </form:form>
 
   </body>
 
   
 
</html>

WEB-INF/pages/final.htm.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<html>
 
   <head>
 
      <title>Spring Static Page</title>
 
   </head>
 
   <body>
 
      <h2>A simple HTML page</h2>
 
   </body>
 
</html>

WebController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.tutorialspoint;
 
import org.springframework.stereotype.Controller;
 
import org.springframework.web.bind.annotation.RequestMapping;
 
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller
 
public class WebController {
 
   @RequestMapping(value = "/index", method = RequestMethod.GET)
 
   public String index() {
 
      return "index";
 
   }
 
   @RequestMapping(value = "/staticPage", method = RequestMethod.GET)
 
   public String redirect() {
 
      return "redirect:/pages/final.htm";
 
   }
 
}

 

Kaynakça:

  • tutorialspoint

Bir cevap yazın Cevabı iptal et

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Kategoriler

  • Java (3)
  • Know How (1)

Son Yazılar

  • REST Servis Standartları
  • Dependency Injection Nedir?
  • Spring MVC
  • Spring Framework’e Genel Bakış

Arşivler

  • Ağustos 2019
  • Temmuz 2019
  • Eylül 2018
©2019 Software Engineering Notes | Powered by WordPress & Superb Themes