You are on page 1of 5

LAB2: HELLO WORLD

MC TIU
Kt thc bi thc hnh, bn c kh nng
To mt project Spring MVC vi eclipse
Vit mt Controller
To mt View
Cch chia s d liu gia Controller v View thng qua model

M T

Nhp [Say Hello]

Hnh: Input.htm

Hnh: submit hello.htm

THC HIN
Bc 1: To d n

Hnh: To d n

Nguyn Nghim nghiemn@fpt.edu.vn

Trang 1

LAB2: HELLO WORLD

Hnh: t tn d n

Bc 2: Cu hnh d n
Chp web.xml, dispatcher-servlet.xml, index.jsp v lib/*.jar t SpringMVCBlank vo v tr tng ng ca
SpringMVCHello

Hnh: Cu trc d n sau khi b sung t SpringMVCBlank

Bc 3: B sung cc thnh phn


Thm cc lp HelloController, UserInfo v 2 file jsp HelloInput.jsp v HelloSuccess.jsp vo cc v tr nh
hnh sau

Nguyn Nghim nghiemn@fpt.edu.vn

Trang 2

LAB2: HELLO WORLD

Hnh: Cu trc y ca d n

Bc 4: Vit m
Son ni dung cho cc thnh phn va thm vo nh sau:
HelloController.java
@Controller
public class HelloController {
@RequestMapping(value="input")
public String showForm(ModelMap model) {
model.addAttribute("user", new UserInfo());
return "HelloInput";
}
@RequestMapping(value="hello")
public String sayHello(@ModelAttribute("user") UserInfo user) {
return "HelloSuccess";
}
}

Cc thnh phn quan trng


o
o
o
o
o

@Controller: ch ra class ny l controller


@RequestMapping(value=action) nh ngha mt action
model.addAttribute(name, value): thm mt attribute vo model
@ModelAttribute("user"): thm i tng user vo model
return ViewName: chn view hin th

Nguyn Nghim nghiemn@fpt.edu.vn

Trang 3

LAB2: HELLO WORLD


UserInfo.java
Lp sau c s dng cha thng tin user c nhp vo form.
public class UserInfo {
String id;
String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

HelloInput.jsp
<%@page pageEncoding="utf-8" contentType="text/html; charset=utf-8" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Spring MVC Seminar 2014</title>
</head>
<body>
<h2>Hello Form</h2>
<form:form action="hello.htm"
method="post" modelAttribute="user">
<div>ID:</div>
<form:input path="id"/>
<div>Name:</div>
<form:input path="name"/>
<hr>
<input type="submit" value="Say Hello">
</form:form>
</body>
</html>

o
o
o

<%@taglib%> khai bo th vin th xy dng form


<form:form> to form. Thuc tnh modelAttribute ch ra bean c s dng hin th ln form
<form:input> to nhp. Thuc tnh path=property tn thuc tnh ca bean UserInfo

HelloSuccess.jsp
<%@page pageEncoding="utf-8" contentType="text/html; charset=utf-8" %>
<!DOCTYPE html>

Nguyn Nghim nghiemn@fpt.edu.vn

Trang 4

LAB2: HELLO WORLD


<html>
<head>
<meta charset="UTF-8">
<title>Spring MVC Seminar 2014</title>
</head>
<body>
<h2>Hello Success</h2>
<ul>
<li>${user.id}</li>
<li>${user.name}</li>
</ul>
</body>
</html>

${user.id}: hin th thuc tnh id ca bean user t trong model

Index.jsp
<jsp:forward page="input.htm"/>

Bc 4: Nng cp
o
o

Thm nhp email vo form


Hin th email nhp vo ln trang kt qu

Nguyn Nghim nghiemn@fpt.edu.vn

Trang 5

You might also like