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 المترادف للـ action الذي يجب معالجته هو الصفحة الخلفية الخاصة بنا، 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 نمایانگر این است که این کلاس یک لایه کنترلر است public class Login { /** * @RequestParam注解的作用是:بر اساس نام پارامتر از URL مقدار پارامتر را دریافت میکند * @param username * نام کاربری، باید با نام فیلد فرم مطابقت داشته باشد * @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">آسف</font>، لا يوجد مستخدم ${username} <br /> <a href="login.jsp" rel="external nofollow" >أعد المحاولة!</a> </body> </html>
اختبار
أدخل في متصفحك http://localhost:8080/SpringTest/login.jsp
بعد ذلك، يمكنك اختبار الكود. لقد تم اختباره شخصيًا وهو يعمل بشكل جيد، لذا لن أقوم بتحميل الصور هنا.
الخلاصة
إليكم بعض النصائح حول الاستخدامات
@Controller يمثل في springMVC-servlet.xml
شكرًا على القراءة، آمل أن تكون مفيدًا لكم، شكرًا لدعمكم لموقعنا!