English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
SpringMVCのログイン小例
準備作業
ディレクトリ構造は以下の通りです
初心者にとっては、プロジェクトの完全なディレクトリ構造がどれだけ幸せなことか。
ディレクトリ構造
個人的な意見:springMVCに注意してください。-servlet.xmlの場所。およびソースコードのパッケージ名。
コード実践
まずは大管家、web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-_app_3_1.xsd" id="WebApp_ID" version="3.1> <display-name>SpringTest</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>*.spring</url-pattern> </servlet-mapping> </web-app>
次に小さな管理人springMVC-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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!-- 最も簡単な設定、Springが自分で探索するように--> <context:component-scan base-package="controller"></context:component-scan> </beans>
再びログイン画面があります、login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8> <title>ログイン画面</title> </head> <body> <form action="login.spring" method="post"> ユーザー名:<input type="text" name="username"><br /> パスワード:<input type="password" name="password"><br /> <input type="submit" value="ログイン"> </form> </body> </html>
login.jspに対応するアクションは、処理するバックエンドページであり、私たちのLogin.Javaです:
package controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller // @Controllerは、このJavaクラスがコントローラレイヤであることを示します public class Login { /** * @RequestParam注解の役割は、パラメータ名に基づいてURLからパラメータ値を取得することです * @param username * ユーザー名は、フォームのnameに一致する必要があります * @param password * ユーザーパスワードも、フォームのデータ項目に対応する必要があります * @param model * オブジェクト領域、データ値を保存するために使用できます * @return */ @RequestMapping("/login") // @RequestMapping 注解を使って指定されたURLパスでこのコントローラレイヤにアクセスできます public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model) { if (username.equals("admin") && password.equals("admin")) { model.addAttribute("username", username); return "ok.jsp"; } else { model.addAttribute("username", username); return "no.jsp"; } } }
最後にok.jspとno.jspがあります:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8> <title>ここにタイトルを挿入してください</title> </head> <body> <font color="green">${username } </font>ようこそ! </body> </html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8> <title>ここにタイトルを挿入してください</title> </head> <body> <font color="red">Sorry</font>、${username }というユーザーはいません! <br /> <a href="login.jsp" rel="external nofollow" >もう一度試してください!</a> </body> </html>
テスト
ブラウザにhttp:を入力します//localhost:8080/SpringTest/login.jsp
それでは、コードをテストすることができます。私自身が実際にテストしており、問題ありませんので、画像を貼り付ける必要はありません。
まとめ
注釈の小技です:
@ControllerはspringMVCに対応しています-servlet.xml内の
ご覧いただきありがとうございます。皆様のサポートに感謝します!