English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring MVCモデルインターフェース

Spring MVCでは、モデルはアプリケーションデータを含むコンテナで動作します。ここでは、データはオブジェクト、文字列、データベースの情報など、どんな形式でもできます。

以下のものを追加する必要があります。 Model インターフェースをコンピュータの制御部分に配置します。アプリケーション。 HttpServletRequest オブジェクトがユーザーが提供する情報を読み取り、それを Model インターフェース。今や、ビューページはモデルパーツ内のデータを簡単にアクセスできます。

モデルインターフェースメソッド

メソッド説明
Model addAllAttributes(Collection <?> arg)提供されたコレクションのすべての属性をこのMapに追加します。
Model addAllAttributes(Map <String,?> arg)提供されたMapのすべての属性をこのMapに追加します。
Model addAllAttribute(Object arg)提供された属性をこのMapに生成された名前で追加します。
Model addAllAttribute(String arg0, Object arg1)属性を提供された名前とバインドします。
Map<String, Object> asMap()現在のモデル属性セットをMapとして返します。
Model mergeAttributes(Map<String,?> arg)追加されたMapのすべての属性をこのMapに追加し、同名の既存オブジェクトが優先されます。
boolean containsAttribute(String arg)このモデルが指定された名前の属性を含んでいるかどうかを示します。

Spring MVCモデルの例

ユーザー名とパスワードを含むログインページを作成します。ここでは、特定の値を使用してパスワードを検証します。

1、依存関係をpom.xmlに追加します。

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>  
    <groupId>javax.servlet</groupId>  
    <artifactId>servlet-api</artifactId>  
    <version>3.0-alpha-1</version>  
</dependency>

2、リクエストページの作成

ここでは、ユーザーの名前とパスワードを受け入れるログインページを作成します。

index.jsp

<html>
<body>
<form action="hello">
ユーザー名: <input type="text" name="name"/> <br><br>
パスワード: <input type="text" name="pass"/> <br><br> 
<input type="submit" name="submit">
</form>
</body>
</html>

3、コントローラークラスの作成

コントローラークラス中で:

HttpServletRequest ユーザーが提供したHTMLフォームデータを読み取るために使用されます。 モデルデータを含め、それをページに表示する。

HelloController.java

package com.w3codebox;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
  @RequestMapping("/hello")
  public String display(HttpServletRequest req,Model m)
  {
    //read the provided form data
    String name=req.getParameter("name");
    String pass=req.getParameter("pass");
    if(pass.equals("admin"))
    {
      String msg="Hello "+ name;
      //モデルにメッセージを追加する
      m.addAttribute("message", msg);
      return "viewpage";
    }
    else
    {
      String msg="Sorry "+ name+". 不正なパスワードを入力しました";
      m.addAttribute("message", msg);
      return "errorpage";
    } 
  }
}

4、web.xmlファイルでコントローラーのエントリを提供する

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" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC</display-name>
   <servlet>  
    <servlet-name>spring</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>    
</servlet>  
<servlet-mapping>  
    <servlet-name>spring</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>  
</web-app>

5、xmlファイルでBeanを定義する

spring-servlet.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: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.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  <!-- コンポーネントスキャニングのサポートを提供する -->
  <context:component-scan base-package="com.w3codebox" />
  <!--変換、フォーマット、バリデーションのサポートを提供 -->
  <mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/></property>
        <property name="suffix" value=".jsp"></property>        
     </bean>
</beans>

6、他のビューコンポーネントを作成

この例を実行するには、以下のビューコンポーネントがWEB-INF/jsp ディレクトリにあります。

viewpage.jsp

<html>
<body>
${message}
</body>
</html>

errorpage.jsp

<html>
<body>
${message}
<br><br>
<jsp:include page="/index.jsp></jsp:include>
</body>
</html>

出力: