By GokiSoft.com| 19:19 09/10/2020|
Spring MVC

[Share Code] Hướng dẫn tạo dự án Spring MVC - Tạo Model, View, Controller - Tìm hiểu URL - Lập trình Spring MVC



Nội Dung
- Tạo project spring mvc (model, view, controller)
- Cấu hình
- Controller + Model + View
- Bài ví:
	- Tạo 1 website hiển thị danh sách sinh viên.

=======================================
Tao 1 page: welcome => Hello World!!!

Path: http://localhost:8080/SpringExample01/welcome.html
Route: welcome
View: welcome.jsp
Controller: WelcomeController => gọi view welcome.jsp

- Gui du lieu tu controller sang view
- Tao 1 model => gui du lieu sang view
- Hoc cach phan tich URL.
	- http://localhost:8080/SpringExample01/news/{tieu-de-bai-viet}.html
	
=====================================
FORM =>GET/POST




#dispatcher-servlet.xml


<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    <context:component-scan base-package="aptech.controller"></context:component-scan>
    <mvc:annotation-driven/>
    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.html">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/views/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>


#cal.jsp


<%-- 
    Document   : cal
    Created on : Oct 9, 2020, 7:26:58 PM
    Author     : student
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Result: ${result}!</h1>
    </body>
</html>


#index.jsp


<%@page contentType="text/html" 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>Welcome to Spring Web MVC project</title>
    </head>

    <body>
        <p>Hello! This is the default welcome page for a Spring Web MVC project.</p>
        <p><i>To display a different welcome page for this project, modify</i>
            <tt>index.jsp</tt> <i>, or create your own welcome page then change
                the redirection in</i> <tt>redirect.jsp</tt> <i>to point to the new
                welcome page and also update the welcome-file setting in</i>
            <tt>web.xml</tt>.</p>
    </body>
</html>


#news.jsp


<%-- 
    Document   : news
    Created on : Oct 9, 2020, 7:24:23 PM
    Author     : student
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World ${hrefParam}!</h1>
    </body>
</html>


#welcome.jsp


<%-- 
    Document   : welcome.jsp
    Created on : Oct 9, 2020, 6:51:21 PM
    Author     : student
--%>

<%@page contentType="text/html" 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>JSP Page</title>
    </head>
    <body>
        <h1>Hello World ${msg}!</h1>
        <ul>
            <li>Name: ${student.name}</li>
            <li>RollNo: ${student.rollno}</li>
            <li>Address: ${student.address}</li>
            <li>Gender: ${student.gender}</li>
            <li>Birthday: ${student.birthday}</li>
        </ul>
    </body>
</html>


#Student.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.model;

/**
 *
 * @author student
 */
public class Student {
    String name, rollno, address, birthday, gender;

    public Student() {
    }

    public Student(String name, String rollno, String address, String birthday, String gender) {
        this.name = name;
        this.rollno = rollno;
        this.address = address;
        this.birthday = birthday;
        this.gender = gender;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRollno() {
        return rollno;
    }

    public void setRollno(String rollno) {
        this.rollno = rollno;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
    
    
}


#WelcomeController.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.controller;

import aptech.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author student
 */
@Controller
@RequestMapping(value = "/")
public class WelcomeController {
    
    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    public String showWelcomePage(ModelMap modelMap) {
        String msg = "TRAN VAN DIEP";
        
        modelMap.put("msg", msg);
        
        Student std = new Student("TRAN VAN A", "R001", "Nam Dinh", "06/06/1996", "Nam");
        modelMap.put("student", std);
        
        return "welcome";
    }
    
    @RequestMapping(value = "/trang-chu", method = RequestMethod.GET)
    public String showHome() {
        return "index";
    }
    
    @RequestMapping(value = "/news/{href_param}", method = RequestMethod.GET)
    public String showDetailNews(@PathVariable("href_param") String hrefParam, ModelMap modelMap) {
        modelMap.put("hrefParam", hrefParam);
        
        return "news";
    }
    
    @RequestMapping(value = "/cal/x/{x}/y/{y}", method = RequestMethod.GET)
    public String showCalculator(@PathVariable("x") float x, @PathVariable("y") float y, ModelMap modelMap) {
        float result = x + y;
        
        modelMap.put("result", result);
        
        return "cal";
    }
}


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)