0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Spring Frameworkを使ってみる(3)

Last updated at Posted at 2020-06-07

画面間の値の受け渡し

index.jspを編集

index.jsp
<!DOCTYPE html>
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form"%>
 
<html>
 <head>
 <meta charset="utf-8">
 <title>ログイン</title>
 </head>
 <body>
 <f:form modelAttribute="loginForm" action="login" method="post">
 <div>
 ユーザ名:<input type="text" id="loginName" name="loginName">
 </div>
 <div>
 パスワード:<input type="text" id="loginPassword" name="loginPassword">
 </div>
 <div>
 <input type="submit" value="ログイン">
 </div>
 </f:form>
 
 </body>
</html>

Loginformを作成

Loginform.java
package com.example.HelloWorld;
 
public class LoginForm {
    private String loginName;
    private String loginPassword;
 
    public String getLoginName() {
        return loginName;
    }
 
    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }
 
    public String getLoginPassword() {
        return loginPassword;
    }
 
    public void setLoginPassword(String loginPassword) {
        this.loginPassword = loginPassword;
    }
}

LoginControllerを編集

LoginController.java
package com.example.HelloWorld;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller
public class LoginController {
	@RequestMapping(value = "/login", method = RequestMethod.POST)
		public String login(Model model, @ModelAttribute("loginForm") LoginForm loginForm) {
		model.addAttribute("loginName", loginForm.getLoginName());
		return "top";
	}
}

@ModelAttribute(“loginForm”)」が、「index.jsp」の中で指定した「modelAttribute」とマッピングされます。そして、その内容は「LoginForm」クラスの同名の変数へと格納されていきます。格納された値を次の画面へ引き継ぐために「”loginName”」という値でModelに追加しています。

top.jspを編集

top.jsp
<!DOCTYPE html>
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
 
<html>
 <head>
 <meta charset="utf-8">
 <title>トップ</title>
 </head>
 <body>
 ようこそ<c:out value="${loginName}" />さん
 </body>
</html>

サーバを立ち上げると、下記画面が表示され、適当なloginNameを入力し、ログインを押下します。
image.png

下記の画面が表示されます。
image.png

次回

入力された値のチェックを実装していきます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?