VietTuts

Tự Học Lập Trình Online

  • Home
  • Java
  • Servlet
  • JSP
  • Struts2
  • Hibernate
  • Spring
  • MyBatis
  • Java WS
  • C
  • C++
  • Python
  • PHP
  • Eclipse
  • VBA
  • Web
    • JavaScript
    • JQUERY
    • JSON
    • AJAX
    • CSS
    • HTML
    • Node.js
    • Angular 7
  • SQL
    • MySQL
    • SQL Server
  • Misc
    • Phần mềm tiện ích
    • Cấu trúc dữ liệu và giải thuật
    • Học lập trình C#
    • Selenium Test

Spring Tuts

Spring là gì? Kiến trúc Spring Spring - Thiết lập môi trường Spring HelloWorld Example Dependency Injection trong Spring IoC Containers trong Spring Định nghĩa Bean trong Spring Phạm vi của Bean trong Spring Vòng đời của Bean trong Spring Spring - Kế thừ Bean Spring - Inner Bean Spring - Injecting Collection Spring - Beans Auto-Wiring Annotation Based Configuration Spring - Java Based Configuration Spring - Event Handling in Spring Spring - Custom Events in Spring Spring - AOP with Spring Framework Spring - JDBC Framework Spring - Transaction Management Spring - Web MVC Framework Spring - Logging with Log4J

Spring Tool

Cài đặt Spring Tool Suite (STS) trong Eclipse Tạo Spring project bằng Spring Tool Suite trong Eclipse Tạo Spring project (annotation) bằng Spring Tool Suite (STS)

Spring 4 Example

Ví dụ login trong Spring 4 Web MVC – Hibernate 4 XML Mapping Ví dụ login trong Spring 4 Web MVC – Hibernate 4 Annotation

Tạo Spring project (annotation) bằng Spring Tool Suite (STS)


Previous
Next

Bài này sẽ hướng dẫn bạn cách tạo Spring project (annotation) bằng Spring Tool Suite (STS) trong Eclipse. Đầu tiên, bạn cần phải tạo Spring project với cấu hình mặc định là XML. Project có cấu trúc như sau:

Cài đặt Spring Tool Suite (STS) trong Eclipse

Nội dung chính

  • Các bước tạo Spring project (annotation) bằng Spring Tool Suite (STS)
  • Run ứng dụng
  • Download Project

Các bước tạo Spring project (annotation) bằng Spring Tool Suite (STS)

Sau đây là các bước tạo Spring Web project với cấu hình annotation.

1. Xóa các file cấu hình XML ở trong project trên như sau:

/spring/application-config.xml

/logback.xml

/WEB-INF/mvc-config.xml

2. Tạo file cấu hình annotation

File: ApplicationConfig.java

package vn.viettuts.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = { "vn.viettuts" })
public class ApplicationConfig {
    // define beans
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver 
                = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
}

3. Cấu hình để sử dụng annotation trong file web.xml

File: web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <display-name>spring-annotation-example</display-name>
    
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <!-- 
        annotated that this application is config by annotation, 
        instead of default by xml 
    -->
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>
    <!-- indicate spring annotation configuration class -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>vn.viettuts.configuration.ApplicationConfig</param-value>
    </context-param>
    
    <!--
 - Servlet that dispatches request to registered handlers 
        (Controller implementations).
     -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

4. Tạo controller và view

File: HelloWorldController.java

package vn.viettuts.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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;

@Controller
public class HelloWorldController {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ModelAndView hello(HttpServletRequest request, 
            HttpServletResponse response) {
        ModelAndView view = new ModelAndView("showMessage");
        return view;
    }
}

File: index.jsp

<!DOCTYPE html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>

<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
  <a href="hello">Hello</a>
</body>
</html>

File: showMessage.jsp

<!DOCTYPE html>

<%@ page language="java" contentType="text/html; charset=UTF-8" 
pageEncoding="UTF-8"%>

<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
  <h2>Hello World!</h2>
</body>
</html>

5. Update file pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework.samples.service.service</groupId>
    <artifactId>spring-annotation-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <!-- Generic properties -->
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- Web -->
        <servlet.version>2.5</servlet.version>
        <!-- Spring -->
        <spring-framework.version>4.3.6.RELEASE</spring-framework.version>
    </properties>

    <dependencies>
        <!-- Spring MVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>
        <!-- Other Web dependencies -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

Cấu trúc project sau khi hoàn thành các bước trên như sau:

Tạo Spring project (annotation) bằng Spring Tool Suite (STS)

Run ứng dụng

Tạo Spring project (annotation) bằng Spring Tool Suite (STS)

Click link

Tạo Spring project (annotation) bằng Spring Tool Suite (STS)

Download Project

Download Now!

Previous
Next

Recent Updates

SpringLayout trong Java SwingMyBatis Example - MyBatis + SpringSpring là gì?Cài đặt Spring Tool Suite (STS) trong EclipseDependency Injection trong SpringĐịnh nghĩa Bean trong SpringInjecting Collection trong SpringInner Bean trong SpringIoC Container trong SpringKế thừa Bean trong SpringKiến trúc SpringPhạm vi của Bean trong Spring

VietTuts on facebook

Học Lập Trình Online Miễn Phí - VietTuts.Vn

Danh sách bài học

Học java
Học servlet
Học jsp
Học Hibernate
Học Struts2
Học Spring
Học SQL

Câu hỏi phỏng vấn

201 câu hỏi phỏng vấn java
25 câu hỏi phỏng vấn servlet
75 câu hỏi phỏng vấn jsp
52 câu hỏi phỏng vấn Hibernate
70 câu hỏi phỏng vấn Spring
57 câu hỏi phỏng vấn SQL

About VietTuts.Vn

Hệ thống bài học trên VietTuts.Vn bao gồm các bài lý thuyết và thực hành về các công nghệ java và công nghệ web. Các bài lý thuyết trên hệ thống VietTuts.Vn được tham khảo và tổng hợp từ các trang http://javatpoint.com, http://www.tutorialspoint.com, http://docs.oracle.com/en …

Scroll back to top

Copyright © 2016 VietTuts.Vn all rights reserved. | VietTuts.Vn team | Liên hệ | Chính sách - riêng tư | sitemap.html | sitemap_index.xml