1. Trang chủ
  2. » Giáo án - Bài giảng

Thực hành lập trình ứng dụng với java

156 31 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

T Biên www.hutech.edu.vn *1.2023.CMP3025* tailieuhoctap@hutech.edu.vn : I .I V SPRING MVC 1.1 1.1.1 H tr x lý form Spring MVC 1.1.2 Vi t code cho Model Class (form-backing object) 1.1.3 Vi t code cho Registration Form dùng Spring Form Tags 1.1.4 Vi t code cho l p Controller 1.1.5 Vi t code cho trang ký thành công (Registration Success Page) 1.1.6 Xem c u trúc project Eclipse t p tin pom.xml 1.1.7 Ch y th ng d ng 10 1.2 11 1.2.1 Hi n th c Bean Validation API Hibernate Validator 12 1.2.2 Spring MVC có h tr cho Bean Validation API 13 1.2.3 Vi t code l p Model 14 1.2.4 Vi t code cho Input Form b ng JSP 15 1.2.5 Vi t code l p Controller 16 1.2.6 Vi t code trang k t qu Result Page b ng JSP 17 1.2.7 C u hình ng c nh ng d ng Spring MVC 18 1.2.8 Vi t t p tin messages.propreties 18 1.2.9 C u hình Spring framework t p tin web.xml 18 1.2.10 Xem xét c u trúc project t p tin JAR c n thi t 19 1.2.11 Ch y th ng d ng 21 BÀI 2: DOWNLOAD VÀ UPLOAD FIL 2.1 23 23 2.1.1 T o Eclipse project 25 2.1.2 T o upload form 28 2.1.3 C u hình Spring MVC MultipartResolver 29 2.1.4 Tri n khai Spring controller 32 2.1.5 T o result page error page 34 2.1.6 Tri n khai ki m tra 2.2 ng d ng (application) 36 39 2.2.1 Code c a trang download 40 2.2.2 Code c a l p Spring controller 40 2.2.3 Vi t code cho t p tin Spring configuration 42 2.2.4 Code c a t p tin web.xml 43 2.2.5 Các t p tin jar c n thi t 43 II 2.2.6 Ch y th ng d ng .44 - 3.1 45 - 45 3.1.1 H tr e-mail c a Spring framework 46 3.1.2 T p tin jar b t bu c 48 3.1.3 T o form g i e-mail 49 3.1.4 C u hình cài t máy ch SMTP Spring MVC 50 3.1.5 T o l p controller Spring MVC 52 3.1.6 Trang k t qu (result page) trang báo l i (error page) 53 3.1.7 Ch y ng d ng 54 3.2 - 55 3.2.1 Các t p tin jar b t bu c 56 3.2.2 Vi t form g i e-mail (có t p tin 3.2.3 nh c u hình Spring MVC kèm) 57 t i lên t p tin e-mail 58 3.2.4 Tri n khai controller b ng Spring MVC 60 3.2.5 Trang k t qu (result page) trang l i (error pages) 62 3.2.6 nh c u hình t p tin mơ t tri n khai web 63 64 4.1 LAB1: JDBC TEMPLATE TRONG SPRING MVC 64 4.1.1 T o s d li u b ng MySQL 64 4.1.2 T o Maven Project b ng Eclipse .65 4.1.3 Vi t code cho l p Model 67 4.1.4 Vi t code cho l p DAO .68 4.1.5 Vi t code t p tin Spring MVC Configuration 71 4.1.6 C u hình Spring MVC Dispatcher Servlet 73 4.1.7 Vi t code cho l p Spring Controller 74 4.1.8 Vi t code cho trang li t kê Contact (Home Page) 75 4.1.9 Vi t code cho trang Contact Form 76 4.2 77 4.2.1 Cách Spring h tr tích h p Hibernate .78 4.2.2 Thi t l p m t Project 78 4.2.3 Vi t code l p Model c u hình Hibernate Mapping 81 4.2.4 Vi t code l p DAO 82 4.2.5 C u hình Spring Application Context 83 4.2.6 Vi t code cho l p Controller .85 4.2.7 Vi t code cho View Page 86 4.2.8 Ch y th 4.3 ng d ng .87 88 4.3.1 Bootstrapping Spring Dispatcher Servlet 89 4.3.2 Ánh x Model Class s d ng JPA Annotations .90 4.3.3 M r ng l p DAO 90 III 4.3.4 C u hình Spring Application Context b ng Java 92 4.3.5 C p nh t Spring Controller Class 95 4.3.6 C p nh t trang li t kê User 97 4.3.7 Vi t code cho trang User Form 97 4.3.8 Ch y th ng d ng 100 101 5.1 101 5.1.1 C u hình Dependencies Maven 102 5.1.2 C u hình thu c tính Configure Database persistence.xml 102 5.1.3 C u hình EntityManagerFactory Transaction Manager 103 5.1.4 Vi t code cho l p Model 104 5.1.5 Vi t code cho Repository Interface 105 5.1.6 Vi t code l p Service 107 5.1.7 Vi t code 5.2 5.2.1 T o th nghi m ng d ng cho Spring Data JPA 108 109 s d li u 110 5.2.2 T o Project Eclipse 110 5.2.3 T O JPA CONFIGURATION FILE 112 5.2.4 VI T CODE L P MODEL 113 5.2.5 VI T CODE CÁC L P C U HÌNH 114 5.2.6 VI T CODE RESPOSITORY INTERFACE 117 5.2.7 Vi t code l p Service 117 5.2.8 Vi t code l p Spring MVC Controller 118 5.2.9 Vi t code li t kê khách hàng (CUSTOMER) 119 5.2.10 Vi t code t o customer m i 120 5.2.11 Vi t code ch nh s a thông tin Customer 122 5.2.12 Vi t code xóa Customer 123 5.2.13 Vi t code tìm ki m Customer 124 127 6.1 LAB1: 127 6.1.1 Gi i thi u v Spring Security 127 6.1.2 Ví d m u v ng d ng Spring Security 128 6.1.3 T o m t Dynamic Web Project v i Maven 128 6.1.4 Thêm vào Maven Dependencies 129 6.1.5 Vi t code cho trang Index (Index Page) 130 6.1.6 Vi t code cho trang Admin (Admin Page) 130 6.1.7 Vi t code cho Spring MVC Controller 131 6.1.8 C u hình Web Deployment Descriptor (web.xml) 132 6.1.9 C u hình Spring MVC Application Context 133 6.1.10 C u hình Spring Security 134 6.1.11 C u trúc toàn b project 135 IV 6.1.12 Ch y ki m tra ng d ng Spring Security m u 135 6.2 137 6.2.1 Thi t l p Project 138 6.2.2 Vi t code cho trang Index Page 138 6.2.3 Vi t code trang Admin (Admin Page) 139 6.2.4 Vi t code cho Spring MVC Controller 140 6.2.5 C u hình Spring Dispatcher Servlet 140 6.2.6 C u hình Spring Security Filter 141 6.2.7 C u hình Authentication Authorization cho ng d ng Spring MVC 142 6.2.8 Configuring Spring MVC Application Context 144 6.2.9 C u trúc toàn b Project 144 6.2.10 Ch y ki m tra ng d ng Spring Security m u 145 TÀI 148 V MƠ MƠN hành trình ng Java mơn cho ngơn cung xây web Java theo mơ hình MVC (Model-View-Control) framework, có trình Java web Spring MVC Mơn trang web qua mơn có giúp cho project Maven Project, Dynamic Web Project; án Java web cách ngôn cách Hibernate XML thêm, xóa, website upload tin, DUNG trình cách lên trang web; xác decorator cách trình dùng; sitemesh trình e-mail HÀNH - Bài 1: - Bài 2: Download upload - Bài 3: - Bài 4: - Bài 5: - Bài 6: trình ký, JDBC, JPA, trình bày phân trang (pagination); lý form Spring MVC mail tin tin Java (Java-based) công Spring Security phân template cho pháp code cho code trang web có xác Spring Java Spring MVC này, hình cơng Spring MVC Spring MVC JDBC Template Hibernate trang CRUD có Spring Web MVC Security cách cách xây t cách download trang web VI Mơn l hành trình Java trình Java U sinh viên có trình MƠN làm hành lên làm t nhà CÁCH DUNG MƠN mơn này, sau và dung hoàn thành tham bài tiêu hành theo thêm tài viên Ngoài ra, Internet có xây website theo nhóm làm PHÁP Mơn HÀNH giá - hành hàng hành theo yêu - GIÁ thi hành tình hình (50%): Hình tham gia làm giáo viên (50%): Hình thi hành, phù quy BÀI 1: BÀI 1: TRÌNH LÝ FORM TRONG SPRING MVC TIÊU: - trình lý form Spring MVC - Java Form Validation Spring MVC 1.1 LAB 1: L P TRÌNH X phát trình viên, web nói chung hình này: Submit Server dùng yêu LÝ FORM lý form phát công Spring MVC vào vào, thông báo cho xem cách Spring MVC framework registration form hàng ngày tình form web dùng, xác lý Bean Validation API vào nút lý dùng Bài lý form sau sau: logic xây BÀI 1: ORM TRONG SPRING MVC tiên, xem cách 1.1.1 H tr x lý form lý form Spring MVC Spring MVC framework theo mơ hình Model-View-Controller nên form - Spring MVC ba thành Model: chính: model, view controller POJO (Plain Old Java Object) form lý tính liên (object) vào model (model object) - View: Các Spring MVC HTML - quan Controller: hình ràng mơ hình lý u xem và liên lý cho mô / chuyên Bây qua xây ký ký) 1.1.2 Vi t code cho Model Class (form-backing object) User.java code sau: File: src/main/java/net/codejava/spring/model/User.java 10 11 12 13 14 package net.codejava.spring.model; import java.util.Date; public class User { private String username; private String password; private String email; private Date birthDate; private String profession; // getters and setters } L model có profession) liên (username, password, email, birthDate and view (trang JSP) Khi 134 BÀI 6: 17 18 19 20 ta quen Spring MVC, Spring phân tích thích yêu configurations and controllers gói Java tìm thành khai báo cho Spring Spring Và bean tên InternalResourceViewResolver cách phân (resolve) logical view names thành physical view pages 6.1.10 C u hình Spring Security khai báo xác trang nào) (ai có tin spring-security.xml (ai có /WEB-INF code sau: truy 10 11 12 13 14 15 16 17 18 19 20 21 22 có hai - xác khai báo password and role (ROLE_ADMIN cho xác - Trong Spring, dùng (user) hình này) username, dùng có truy , ta khai báo URL Theo hình này, URL 135 BÀI 6: /admin** ROLE_ADMIN có ý phép truy URL yêu (Spring security filter) liên 6.1.11 C u trúc toàn b cùng, /logout Spring yêu HTTP GET project thúc 6.1.12 Ch y ki m tra khai dùng có role (vai trị) trúc project sau: ng d ng Spring Security m u án Tomcat, sau URL sau vào trình http://localhost:8080/SpringSecurityBasicXML Ta trang index (index page) sau: 136 BÀI 6: Trên trang này, vào liên Go to Administrator Page, gõ http://localhost:8080/SpringSecurityBasicXML/admin Spring security page) trang sau: sai tên dùng thông báo Bây hình (default login tên màu dùng này), Spring (username password), Spring security form, hình sau: xác (admin nimda, theo trang t 137 BÀI 6: Bây tra Spring security ta vào liên URL/logout, làm trang Chú ý (logout) Logout, phiên (session) (login page): thông báo You have been logged URL thay thành http://localhost:8080/SpringSecurityBasicXML/login?logout Spring security truy làm phiên (session), vào trang: http://localhost:8080/SpringSecurityBasicXML/admin ta Chúng ta trang Spring security project hình XML 6.2 LAB2: SPRINGMVC SECURITY V I JAVA Bài code Java Spring Web MVC hình 138 BÀI 6: Tham thêm Spring Web MVC Spring Security framework Bây hình Java tìm hình XML cách thích (annotation) Spring Web MVC 6.2.1 Thi t l p Project Ta Eclipse IDE project web (dynamic web project), sau thành Maven project Project Spring MVC Security Java Config công sau nhiên ta có - Java - Tomcat with Servlet 3.1 - Spring framework 4.2.4.RELEASE - Spring Security 4.0.3.RELEASE - JSTL 1.2 - Eclipse IDE, Mars Release (4.5.0) hình cho dependencies 10 11 12 13 14 phát cách phiên Maven liên quan Spring Security sau: org.springframework.security spring-security-web ${spring.security.version} org.springframework.security spring-security-config ${spring.security.version} 6.2.2 Vi t code cho trang Index Page có tên views index.jsp code HTML /WEB-INF, sau sau: tin 139 BÀI 6: 10 11 12 13 14 15 16 Spring Security Basic Demo (Java Config) Spring Security Basic Demo (Java Config) Go to Administrator Page trang hyperlink tiêu trang Security Basic Demo (Java viên (administrator page) yêu xác truy 6.2.3 Vi t code trang Admin (Admin Page) theo, tin admin.jsp /WEB-INF/views code sau: Spring Security Basic Demo (Java Config) 10 11 12 13 ${title} 14 ${message} 15 16 Welcome : ${pageContext.request.userPrincipal.name} | 17 Logout 18 19 20 21 ây trang viên (administrator page) yêu (authentication authorization) tiêu truy thông báo model dùng liên Ta xác dùng (logout link) JSTL tên 140 BÀI 6: MVC SECURITY 6.2.4 Vi t code cho Spring MVC Controller theo, ta code cho Spring controller lý yêu Java có tên MainController gói net.codejava.spring code sau: 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package net.codejava.spring; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; /** * Spring Web MVC Security Java Config Demo Project * Primary controller of the application */ @Controller public class MainController { @RequestMapping(value="/", method = RequestMethod.GET) public ModelAndView visitHome() { return new ModelAndView("index"); } @RequestMapping(value="/admin", method = RequestMethod.GET) public ModelAndView visitAdmin() { ModelAndView model = new ModelAndView("admin"); model.addObject("title", "Admministrator Control Panel"); model.addObject("message", "This page demonstrates how to use Spring security."); return model; } } Controller - lý yêu yêu - context root, yêu trang (requests): trang (home page) viên (administrator page), trang Spring security Các annotation (chú thích) @Controller @RequestMapping báo controller có hai Spring qt chúng tơi x khai lý HTTP GET Các thích hình tin Spring application context file) 6.2.5 C u hình Spring Dispatcher Servlet Thay hình XML mã Java dung code hình sau: web.xml bình chúng tơi Java có tên SpringWebAppInitializer 141 BÀI 6: 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package net.codejava.spring; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext ; import org.springframework.web.servlet.DispatcherServlet; public class SpringWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext(); appContext.register(MvcConfig.class); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } } SpringWebAppInitializeris WebApplicationInitializer Servlet 3.0+ container code mã XML sau web.xml: 10 11 12 13 14 15 SpringController org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/spring-mvc.xml 1 SpringController / 6.2.6 C u hình Spring Security Filter Spring Security Filter ta Thay servlet yêu XML, mã Java Spring Security Filter: 142 10 11 12 13 14 15 16 17 BÀI 6: package net.codejava.spring; import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; /** * Spring Web MVC Security Java Config Demo Project * Bootstraps Spring Security Filter * */ public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { public SecurityWebApplicationInitializer() { super(SecurityConfig.class); } } Nó 10 11 code XML sau web.xml: springSecurityFilterChain org.springframework.web.filter.DelegatingFilterProxy springSecurityFilterChain /* ý SecurityWebApplicationInitializer hàm constructor tìm hình cha constructor), có tên SecurityConfig - SecurityConfig.class cho mô Spring Security bên 6.2.7 C u hình Authentication Authorization cho ng d ng Spring MVC có tên SecurityConfig WebSecurityConfigurerAdapter sau: 10 package net.codejava.spring; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web code 143 BÀI 6: 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 configuration.WebSecurityConfigurerAdapter; /** * Spring Web MVC Security Java Config Demo Project * Configures authentication and authorization for the application */ @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth inMemoryAuthentication() withUser("admin").password("nimda").roles("ADMIN"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() antMatchers("/").permitAll() antMatchers("/admin**").access("hasRole('ADMIN')") and().formLogin(); http.csrf().disable(); } } 10 11 hình sau XML: ý ta vơ hóa CSRF này: http.csrf ().Disable(); yêu GET Ta nên Spring Security CSRF có liên pháp /logout yêu HTTP 144 BÀI 6: 6.2.8 Configuring Spring MVC Application Context Thay cách 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 XML, code Java hình view resolvers Spring MVC sau: package net.codejava.spring; import import import import org.springframework.context.annotation.Bean; org.springframework.context.annotation.ComponentScan; org.springframework.context.annotation.Configuration; org.springframework.web.servlet.view.InternalResourceViewResolver; /** * Spring Web MVC Security Java Config Demo Project * Configures Spring MVC stuffs */ @Configuration @ComponentScan("net.codejava.spring") public class MvcConfig { @Bean(name = "viewResolver") public InternalResourceViewResolver getViewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; } } ta có phân hình bean InternalResourceViewResolver logical view names thành trang JSP lý Nó XML sau: 6.2.9 C u trúc toàn b cùng, trúc Project project IDE Eclipse: 145 BÀI 6: 6.2.10 Ch y ki m tra lúc Sau khai ng d ng Spring Security m u Spring MVC Security mà ta án Tomcat, URL sau vào trình http://localhost:8080/SpringMVCSecurityJavaConfig Trang (home page) xây cho 146 BÀI 6: vào hyperlink Go to Administrator Page, http://localhost:8080/SpringMVCSecurityJavaConfig/admin Spring trang tiên, thơng báo theo, hình trang tên tên sau: dùng màu khơng form, dùng n sau: xác (admin nimda, theo này), Spring security xác viên (administrator page) Spring security sau: thông tin 147 BÀI 6: Bây tra (logout functionality) Logout, Spring security filter (session) ý form URL /logout URL, làm trang phiên (login page): thông báo thay vào liên màu xanh thành http://localhost:8080/ SpringMVCSecurityJavaConfig/login?logout Spring security vơ hóa phiên (session), truy trang http://localhost:8080/SpringMVCSecurityJavaConfig/admin trang (login page) l 148 TÀI THAM [1]Introduction to Spring Web MVC framework https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework reference/html/mvc.html ngày truy [2]Chuyên 28/11/2022 Spring MVC https://stackjava.com/category/spring-mvc ngày truy 28/11/2022 [3]Spring MVC Tutorial https://www.javatpoint.com/spring-mvc-tutorial ngày truy 28/11/2022 [4]Spring MVC Tutorial https://o7planning.org/10959/spring-mvc ngày truy 28/11/2022 [5]Spring - MVC Framework https://www.tutorialspoint.com/spring/spring_web_mvc_framework.htm ngày truy 28/11/2022

Ngày đăng: 28/07/2023, 17:58

Xem thêm:

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w