出力[Spring]MVCコンセプトとHelloWorld
TodayWhatILearedを運営し、この機会を利用して最初に選んだテーマSpringBootをよく学び、熟知します.可能かどうかは分からないけど最善を尽くせばいい
1)ユーザがURLでHTTPリクエストを送信すると,DispatcherServiceletはリクエストを受信する.
2)DispatcherServiceletはこの要求をHandlerMapperに割り当てる.
3)HandlerMapperはControllerを選択し、DispatcherServiceletに渡す.
4)DispatcherServiceletは、どのコントローラが処理するかを知るために要求処理を渡す.
5)コントローラは、要求を処理または確認し、モデルデータを生成する.最後に、ビュー、モデルの名前をDispatcherServiceletに返します.
6)DispatcherServiceletは、アプリケーションのビューを表示するようにViewResolverに要求します.
7)ViewResolverは、実際のビューを検索して返します.
8)したがって、ViewおよびModelは、Viewコンポーネントに送信される.
9)ViewコンポーネントをHTMLにエクスポートし、View、Modelを結合する.
10)最終的には,Browser環境にHTML結果を送信する.
まず、を使用して環境を設定し、Maven Webアプリケーションを作成します. Generateボタンを押し、受け取ったファイルとしてVisual Studioコードで実行します. 上のパッケージは、Webを実行するときにSpring MVCが正常に動作するように、パッケージとして追加する必要があります. java/package名の下にconfigというフォルダAppConfigを作成します.Javaを作成します. @Configurationはclass-level注釈でbeanを定義するために使用されます. @EnableWebMvcでは、XMLベースの実行中にmvc:annotation-drivenと同じ機能を実行できます. @ComponentScanには、複数の@Controllerや@Serviceなどのパッケージが含まれています. ただし、以上の3つのAnnotationを含むのは@SpringBootApplicationなので書かなくても大丈夫です.
基本的にSpring MVCに触れるためには基本的なモデルレベルが必要です. はconfigフォルダと同じ場所にmodelというフォルダを作成することもできます. モデルフォルダと同様に、コントローラフォルダを作成するだけです. 要塞は@GetMapping,@PostMappingを用いて要求の明確性を区別することができるが,以前は@ResquestMappingを用いて明確にすることもできた. model.addAttribute(「コンテンツ」、変数)では、jspが変数をロードし、画面に表示できます. は、適用するビューの名前、すなわちjspの名前を返します. src/main/の下にwebapp/WEB-INF/viewsフォルダを作成し、その下にhelloworldを作成します.jspを作成すればいいです.
最終的にが実行されると、spring initializerに画面が表示され、設定した変数の名前が表示されます. application.propertiesに上記のコードを入れればいいです.
0.MVCの実行手順
1)ユーザがURLでHTTPリクエストを送信すると,DispatcherServiceletはリクエストを受信する.
2)DispatcherServiceletはこの要求をHandlerMapperに割り当てる.
3)HandlerMapperはControllerを選択し、DispatcherServiceletに渡す.
4)DispatcherServiceletは、どのコントローラが処理するかを知るために要求処理を渡す.
5)コントローラは、要求を処理または確認し、モデルデータを生成する.最後に、ビュー、モデルの名前をDispatcherServiceletに返します.
6)DispatcherServiceletは、アプリケーションのビューを表示するようにViewResolverに要求します.
7)ViewResolverは、実際のビューを検索して返します.
8)したがって、ViewおよびModelは、Viewコンポーネントに送信される.
9)ViewコンポーネントをHTMLにエクスポートし、View、Modelを結合する.
10)最終的には,Browser環境にHTML結果を送信する.
1.Maven Webアプリケーションの作成
まず、
2. pom.xmlファイル依存性の追加
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
3. AppConfig.Javaの作成
package com.example.springmvchelloworld.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {
"com.example.springmvchelloworld"
})
public class AppConfig {
@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
4. HelloWorld.Javaの作成
package com.example.springmvchelloworld.model;
public class HelloWorld {
private String message;
private String dateTime;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
}
5. HelloWorldController.Javaの作成
package com.example.springmvchelloworld.controller;
import java.time.LocalDateTime;
import com.example.springmvchelloworld.model.HelloWorld;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HelloWorldController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String handler(Model model) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setMessage("Hello World Example Using Spring MVC 5!!!");
helloWorld.setDateTime(LocalDateTime.now().toString());
model.addAttribute("helloWorld", helloWorld);
return "helloworld";
}
}
6. helloworld.jspの作成
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head><%@ page isELIgnored="false" %>
<meta charset="ISO-8859-1">
<title>Spring 5 MVC - Hello World Example | javaguides.net</title>
</head>
<body>
<h2>${helloWorld.message}</h2>
<h4>Server date time is : ${helloWorld.dateTime}</h4>
</body>
</html>
7.最終運転画面
最終的に
その他の設定:画面が表示されない場合
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
# JSP 수정시 서버 재시작 없이 바로 적용될 수 있게 설정
server.servlet.jsp.init-parameters.development = true
Reference
この問題について(出力[Spring]MVCコンセプトとHelloWorld), 我々は、より多くの情報をここで見つけました https://velog.io/@guri_coding/Spring-MVC-기초-공부하기テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol