By GokiSoft.com|
09:53 12/12/2020|
Spring MVC
[Share Code] Chia sẻ nội dung bài học - Tìm hiểu Spring MVC
#StudentController.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 bean.Student;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
*
* @author teacher
*/
@Controller
@RequestMapping(value = "/student")
public class StudentController {
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String index(ModelMap modelMap) {
List<Student> dataList = new ArrayList<>();
dataList.add(new Student("Sinh vien a", "a@gmail.com", "2000-02-09", "123", "322", "Ha Noi"));
dataList.add(new Student("Sinh vien b", "a@gmail.com", "2000-02-09", "123", "322", "Ha Noi"));
dataList.add(new Student("Sinh vien c", "a@gmail.com", "2000-02-09", "123", "322", "Ha Noi"));
dataList.add(new Student("Sinh vien d", "a@gmail.com", "2000-02-09", "123", "322", "Ha Noi"));
dataList.add(new Student("Sinh vien e", "a@gmail.com", "2000-02-09", "123", "322", "Ha Noi"));
dataList.add(new Student("Sinh vien f", "a@gmail.com", "2000-02-09", "123", "322", "Ha Noi"));
dataList.add(new Student("Sinh vien g", "a@gmail.com", "2000-02-09", "123", "322", "Ha Noi"));
dataList.add(new Student("Sinh vien h", "a@gmail.com", "2000-02-09", "123", "322", "Ha Noi"));
modelMap.put("studentList", dataList);
return "student/list";
}
@RequestMapping(value = "/editor", method = RequestMethod.GET)
public String view() {
return "student/editor";
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@RequestParam Map<String, String> form, ModelMap modelMap) {
//Lay du lieu tu Form gui len server
String name = form.get("name");
String email = form.get("email");
String birthday = form.get("birthday");
String password = form.get("password");
String confirm_pwd = form.get("confirm_pwd");
String address = form.get("address");
Student std = new Student(name, email, birthday, password, confirm_pwd, address);
System.out.println(std);
//Truyen du lieu tu controller -> sang view
modelMap.put("student", std);
return "student/view";
}
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public void delete() {
}
}
#TestController.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 org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
*
* @author teacher
*/
@Controller
@RequestMapping(value = "/test")
public class TestController {
//welcome/index
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public String showWelcome() {
return "welcome";//view
}
//welcome/test
@RequestMapping(value = "/helloworld", method = RequestMethod.GET)
public String showHelloWorld() {
return "index";//view
}
}
#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 bean;
import java.io.Serializable;
/**
*
* @author teacher
*/
public class Student implements Serializable{
String name, email, birthday, password, confirmPwd, address;
public Student() {
}
public Student(String name, String email, String birthday, String password, String confirmPwd, String address) {
this.name = name;
this.email = email;
this.birthday = birthday;
this.password = password;
this.confirmPwd = confirmPwd;
this.address =address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getConfirmPwd() {
return confirmPwd;
}
public void setConfirmPwd(String confirmPwd) {
this.confirmPwd = confirmPwd;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" + "name=" + name + ", email=" + email + ", birthday=" + birthday + ", password=" + password + ", confirmPwd=" + confirmPwd + '}';
}
}
#applicationContext.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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!--bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" /-->
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
</beans>
#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"/>
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="aptech.controller"/>
<!--
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/view/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
#web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
#editor.jsp
<%--
Document : editor
Created on : Dec 12, 2020, 9:13:00 AM
Author : teacher
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Registation Form * Form Tutorial</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"/>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="text-center">Registation Form - Input Student's Detail Information</h2>
</div>
<div class="panel-body">
<form method="post" action="save.html">
<div class="form-group">
<label for="usr">Name:</label>
<input required="true" type="text" class="form-control" id="usr" name="name"/>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input required="true" type="email" class="form-control" id="email" name="email"/>
</div>
<div class="form-group">
<label for="birthday">Birthday:</label>
<input type="date" class="form-control" id="birthday" name="birthday"/>
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input required="true" type="password" class="form-control" id="pwd" name="password"/>
</div>
<div class="form-group">
<label for="confirmation_pwd">Confirmation Password:</label>
<input required="true" type="password" class="form-control" id="confirmation_pwd" name="confirm_pwd"/>
</div>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" name="address"/>
</div>
<button class="btn btn-success">Register</button>
</form>
</div>
</div>
</div>
</body>
</html>
#list.jsp
<%--
Document : list
Created on : Dec 12, 2020, 9:12:47 AM
Author : teacher
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Student List - Page</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"/>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="text-center">Student List - Page</h2>
</div>
<div class="panel-body">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>STT</th>
<th>Ten</th>
<th>Email</th>
<th>Ngay Sinh</th>
<th>Dia Chi</th>
</tr>
</thead>
<tbody>
<c:forEach items="#{studentList}" var="std" varStatus="loop">
<tr>
<td>${loop.index + 1}</td>
<td>${std.name}</td>
<td>${std.email}</td>
<td>${std.birthday}</td>
<td>${std.address}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
#view.jsp
<%--
Document : view
Created on : Dec 12, 2020, 9:20:36 AM
Author : teacher
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@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!</h1>
<ul>
<li>Name: ${student.name}</li>
<li>Email ${student.email}</li>
<li>Birthday: ${student.birthday}</li>
<li>Password: ${student.password}</li>
<li>Address: ${student.address}</li>
</ul>
</body>
</html>
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)