English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Un piccolo esempio di login di SpringMVC
Preparazione
La struttura delle directory è la seguente
Per i principianti, avere una struttura di directory completa di un progetto è una cosa molto felice.
Struttura delle directory
Suggerimento personale: presta attenzione alla posizione di springMVC-servlet.xml. E al nome del pacchetto delle sorgenti.
Pratica di codice
Prima c'è il grande amministratore, 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>
Poiò il piccolo amministratore 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"> <!-- La configurazione più semplice, lascia che Spring esplori da solo --> <context:component-scan base-package="controller"></context:component-scan> </beans>
C'è anche un'interfaccia di accesso, 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>Interfaccia di accesso</title> </head> <body> <form action="login.spring" method="post"> username:<input type="text" name="username"><br /> Password:<input type="password" name="password"><br /><input type="submit" value="Accedi"> </form> </body> </html>
Il file action corrispondente a login.jsp è la pagina di backend da elaborare, ossia il nostro 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 rappresenta questa classe Java come livello di controllo controller public class Login { /** * La funzione dell'annotazione @RequestParam è: ottenere il valore del parametro dal URL in base al nome del parametro * @param username * Il nome utente, deve corrispondere necessariamente al nome del campo del modulo * @param password * La password dell'utente, dovrebbe corrispondere anche alla voce dei dati del modulo * @param model * Un oggetto di dominio, utilizzabile per memorizzare valori di dati * @return */ @RequestMapping("/login") // @RequestMapping annotazione può essere utilizzata per accedere a questo livello di controllo tramite l'URL specificato 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"; } } }
Infine ci sono ok.jsp e 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>Inserisci il titolo qui</title> </head> <body> <font color="green">${username } </font>Benvenuto! </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>Inserisci il titolo qui</title> </head> <body> <font color="red">Scusa</font>,non esiste l'utente ${username }! <br /> <a href="login.jsp" rel="external nofollow" >Riprova!</a> </body> </html>
Test
Inserisci http://localhost:8080/SpringTest/login.jsp nel tuo browser
Poi puoi testare il codice. Ho testato personalmente e funziona bene, non riporterò immagini qui.
Conclusione
Ecco alcuni suggerimenti per l'uso delle annotazioni:
@Controller corrisponde a springMVC-servlet.xml
Grazie per aver letto, spero di essere stato d'aiuto, grazie per il supporto al nostro sito!