1. Trang chủ
  2. » Công Nghệ Thông Tin

Xây dựng demo cho ứng dụng spring

15 270 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

Cấu trúc

  • 1.1. Dynamic Web project

    • 1.1.1. Create java web application

    • 1.1.2. Xây dựng controller

    • 1.1.3. Cấu hình thư mục jsp

    • 1.1.4. Sử dụng Sitemap

  • 1.2. Connect Database

    • 1.2.1. Sử dụng iBatis

  • 1.3. Displaytag, jstl & một số thư viện khác

Nội dung

Phiên bản: 0.1 Spring application 2013-12-10 SPRING APPLICATION XÂY DỰNG DEMO CHO ỨNG DỤNG SPRING THUỘC TÍNH TÀI LIỆU Tiêu đề: Xây dựng demo cho ứng dụng Spring Chủ đề: Spring application Tác giả: Laptrinh.vn Từ khóa: Spring, ibatis Phiên bản: 0.1 Spring application 2013-12-10 TABLE OF CONTENTS THUẬT NGỮ GIỚI THIỆU XÂY DỰNG ỨNG DỤNG .3 1.1 Dynamic Web project 1.1.1 Create java web application 1.1.2 Xây dựng controller 1.1.3 Cấu hình thư mục jsp 1.1.4 Sử dụng Sitemap 1.2 Connect Database .9 1.2.1 Sử dụng iBatis .9 1.3 Displaytag, jstl & số thư viện khác 13 Phiên bản: 0.1 THUẬT NGỮ This section is intentionally left blank GIỚI THIỆU XÂY DỰNG ỨNG DỤNG 1.1 Dynamic Web project 1.1.1 Create java web application Sử dụng thư viện Spring Download: http://www.springsource.org/spring-framework Copy vào thư mục lib Spring application 2013-12-10 Phiên bản: 0.1 Spring application 2013-12-10 Thêm thư viện để ghi log log4j-1.2.17.jar commons-logging-1.1.1.jar Chỉnh sửa file WEB-INF\web.xml Spring dispatcher org.springframework.web.servlet.DispatcherServlet 1 dispatcher *.htm index.html index.htm index.jsp default.html default.htm default.jsp Phiên bản: 0.1 Spring application 2013-12-10 Thêm file WEB-INF\dispatcher-servlet.xml 1.1.2 Xây dựng controller Tạo package springapp.web.controller Tạo class: HelloController package springapp.web.controller; import import import import org.springframework.stereotype.Controller; org.springframework.ui.Model; org.springframework.web.bind.annotation.RequestMapping; org.springframework.web.servlet.ModelAndView; /** * Description: Hello controller * @author DatNH * * Detail: * - @: annotation java * */ @Controller public class HelloController { /** * Nhan request /hello, xu ly thong tin * @return */ @RequestMapping("/hello") public ModelAndView hello(Model model) { // set session value model.addAttribute("name", "DatNH"); return new ModelAndView("hello.jsp"); } @RequestMapping("/helloworld") public String helloworld(Model model) { Phiên bản: 0.1 Spring application 2013-12-10 model.addAttribute("name", "DatNH"); return "hello.jsp"; } } Tạo file WebContent/hello.jsp Hello World Chương trình xây dựng dựa Xin chào: ${name} framework MVC Sping 3 Kết 1.1.3 Cấu hình thư mục jsp Thêm vào file dispatcher-servlet.xml Sửa lại file controller return new ModelAndView("hello"); Tạo thư mục WEB-INF/jsp copy file hello.jsp vào 1.1.4 Sử dụng Sitemap Download: http://www.opensymphony.com/sitemesh Copy lib sitemesh-2.4.x.jar vào thư mục WEB-INF/lib Thêm filter vào web.xml Phiên bản: 0.1 Spring application 2013-12-10 sitemesh com.opensymphony.module.sitemesh.filter.PageFilter sitemesh /* Tạo số thư mục WebContent/ • decorators : Thư mục gồm tất file decorator (e.g main.jsp, printable.jsp) • includes: Thư mục gồm tất file include tới file khác (e.g header.jsp, footer.jsp, copyright.jsp) • images: Thư mục bao gồm tất ảnh (e.g background.gif, logo.gif) • styles: Thư mục bao gồm tất CSS styles (e.g ie4.css, ns4.css) • scripts: Thư mục bao gồm tất scripts (JavaScript, VBScript files) Tạo file WEB-INF/decorators.xml /* Tạo file: /includes/header.jsp [HEADER] Tạo file: /includes/footer.jsp [FOOTER] Tạo file: /includes/taglibs.jsp Phiên bản: 0.1 Tạo file decorator : /decorators/main.jsp [springapp] Kết Cấu trúc project Spring application 2013-12-10 Phiên bản: 0.1 1.2 Connect Database Tải thư viện JDBC driver phù hợp, dùng MySQL Tạo file context.xml META-INF 1.2.1 Sử dụng iBatis Download: http://code.google.com/p/mybatis/ Spring application 2013-12-10 Phiên bản: 0.1 Project Description MyBatis SQL Mapping Framework for Java Spring application 2013-12-10 Links download | docs Generator Code generator for MyBatis and iBATIS download | docs Trong web.xml, tạo reference tới resource SpringMySqlDS Chat Datasource jdbc/SpringMySqlDS javax.sql.DataSource Container Tạo package • springapp.core.dao • springapp.core.dao.xml • springapp.core.model • springapp.core.service Copy mybatis-3.2.0-SNAPSHOT.jar vào WEB-INF/lib Tạo file mybatisConfig.xml springapp.core.dao.xml Cấu hình environments: Tạo file mapping database table java class • Khai báo mapping column table với thuộc tính java • Thực câu lệnh generator cd /d C:\Users\Phoenix\workspace\juno\Spring\generator\ (tùy thư mục nhé) Phiên bản: 0.1 Spring application 2013-12-10 java -jar mybatis-generator-core-1.3.1.jar -configfile generatorConfig.xml -overwrite -tables user Các class generator UserMapper.java UserMapper.xml User.java Xử lý liệu • • Tạo service class o Interface: UserServiceLocal.java o Class: UserService Tạo bean cho UserService o File web.xml Thêm cấu hình applicationContext.xml contextConfigLocation /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener o File applicationContext.xml Application context: Khai báo bean session sử dụng chương trình o File myBatisConfig.xml Phiên bản: 0.1 • Xử lý Controller o File UserController.java package springapp.web.controller; import import import import org.springframework.beans.factory.annotation.Autowired; org.springframework.stereotype.Controller; org.springframework.ui.Model; org.springframework.web.bind.annotation.RequestMapping; import springapp.core.model.User; import springapp.core.service.UserServiceLocal; /** * User controller * @author DatNh * */ @Controller public class UserController { @Autowired private UserServiceLocal userService; @RequestMapping("/user") public String helloworld(Model model) { User user = userService.selectByPrimaryKey(1); model.addAttribute("user", user); return "user"; } } o File user.jsp Hello World User demo Xin chào: ${user.username} Spring application 2013-12-10 Phiên bản: 0.1 o Spring application 2013-12-10 File UserServiceLocal.java package springapp.core.service; import springapp.core.model.User; /** * * @author DatNH * */ public interface UserServiceLocal { User selectByPrimaryKey(int id); } o File UserService.java package springapp.core.service; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import springapp.core.dao.UserMapper; import springapp.core.model.User; /** * User service * @author DatNH * */ public class UserService implements UserServiceLocal { @Override public User selectByPrimaryKey(int id) { SqlSessionFactory sqlMapper = MyBatisService.getSessionFactory(); SqlSession sqlSession = sqlMapper.openSession(true); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.selectByPrimaryKey(id); sqlSession.close(); return user; } } 1.3 Displaytag, jstl & số thư viện khác Copy thư viện displaytag-1.2.jar Phiên bản: 0.1 jstl.jar javax.servlet.jsp.jstl-1.2.1.jar aopalliance-1.0.jar Spring application 2013-12-10 javax.servlet.jsp.jstl-api-1.2.1.jar commons-lang-2.6.jar commons-beanutils-1.8.3.jar File taglibs.jsp @RequestMapping("/user/list") public String list(Model model) { List userList = userService.getAll(); model.addAttribute("userList", userList); return "userList"; } File UserServiceLocal.java List getAll(); File UserService.java @Override public List getAll() { SqlSessionFactory sqlMapper = MyBatisService.getSessionFactory(); SqlSession sqlSession = sqlMapper.openSession(true); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List userList = mapper.getAll(); sqlSession.close(); return userList; } File UserMapper.java List getAll(); File UserMapper.xml select from user Phiên bản: 0.1 Spring application 2013-12-10 File userList.jsp Hello World User List Kết [...]... xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans /spring- beans-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee /spring- jee-2.5.xsd"> o File myBatisConfig.xml Phiên bản: 0.1 • Xử lý Controller o File UserController.java package springapp.web.controller; import import import import org.springframework.beans.factory.annotation.Autowired; org.springframework.stereotype.Controller; org.springframework.ui.Model; org.springframework.web.bind.annotation.RequestMapping; import springapp.core.model.User; import springapp.core.service.UserServiceLocal; /** * User controller... User demo Xin chào: ${user.username} Spring application 2013-12-10 Phiên bản: 0.1 o Spring application 2013-12-10 File UserServiceLocal.java package springapp.core.service; import springapp.core.model.User; /** * * @author DatNH * */ public interface UserServiceLocal { User selectByPrimaryKey(int id); } o File UserService.java package springapp.core.service;... org.springframework.web.context.ContextLoaderListener o File applicationContext.xml Application context: Khai báo các bean session sử dụng trong chương trình ... GIỚI THIỆU XÂY DỰNG ỨNG DỤNG 1.1 Dynamic Web project 1.1.1 Create java web application Sử dụng thư viện Spring Download: http://www.springsource.org /spring- framework Copy vào thư mục lib Spring application... xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans /spring- beans-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee /spring- jee-2.5.xsd">... 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">

Ngày đăng: 03/12/2015, 23:19

TỪ KHÓA LIÊN QUAN

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

TÀI LIỆU LIÊN QUAN

w