SpringMVC實現文本域和下拉列表

首先看一下結果:

在首頁main-form設置Student Form鏈接

在這裏插入圖片描述

進入student/showForm編輯姓名,由於建立了student類保存和獲取姓、名,所以在studentController中的建立student對象,使用@modelAttribute將編輯文本框中的內容和(student)theStudent.lastName,theStudent.firstName綁定,再通過這個StudentController將student-confirmation傳到頁面的時候顯示出來。

在這裏插入圖片描述

在這裏插入圖片描述

下面是代碼:

Student 類

public class Student {
	private String firstName;
	private String lastName;
	
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public Student() {
		}
}

StudentController

@Controller
@RequestMapping("/student")
public class StudentController {
	
	@RequestMapping("/showForm")
	public String showForm(Model theModel) {
		
		//create a student object
		Student theStudent = new Student();
		//add student object to the model
		theModel.addAttribute("student",theStudent);
		return "student-form";
	}
	
	@RequestMapping("/processForm")
	public String processForm(@ModelAttribute("student") Student theStudent) {
		
		System.out.println("theStudent:" + theStudent.getFirstName()
		+" "+theStudent.getLastName());
		
		return "student-confirmation";
	}
}
@Controller
public class HomeController {

	@RequestMapping("/")
	public String showPage() {
		return "main-menu";
		
	}
}

/spring-mvc-demo/WebContent/WEB-INF/view/main-menu.jsp

<!DOCTYPE>
<html>

<body>

<h2>Spring MVC Demo - Home Page</h2>

<hr>

<a href="hello/showForm">Hello World Form</a>

<br><br>

<a href="student/showForm">Student Form</a>
</body>

</html>

/spring-mvc-demo/WebContent/WEB-INF/view/student-form.jsp

<!DOCTYPE HTML>
<html>
<head>
	<title>Student Registration Form</title>
</head>

<body>
	<form:form action="processForm" modelAttribute="student">
	
		First Name: <form:input path="firstName" />
		
		<br><br>
		
		Last Name: <form:input path="lastName" />
		
		<br><br>
		
		<input type = "submit" value="Submit" />
	
	</form:form>
	
</body>
</html>
/spring-mvc-demo/WebContent/WEB-INF/view/student-confirmation.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
 
	<title>Student Confirmation</title>
</head>
<body>

The student is confirmed: ${student.firstName} ${student.lastName}

</body>
</html>

下拉列表(一) //在JSP網頁上編輯

Drop-Down Lists的實現其實只需要在student類添加country的attribute,在student-form.jsp裏面把下拉列表的code加進去,然後在student-confirmation頁面添加顯示country屬性就可以了。

student類

	private String country;
	

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

student-form.jsp

		Last Name: <form:input path="lastName" />
		
		<br><br>
		Country:
		<form:select path="country">
		
			<form:option value="China" label="China" />
			<form:option value="France" label="France" />
			<form:option value="India" label="India" />
			<form:option value="Germany" label="Germany" />
		</form:select
		<br><br>

student-confirmation.jsp


<br><br>
Country: ${student.country}

在這裏插入圖片描述

在這裏插入圖片描述

下拉列表(一) //在java code裏面實現

在這裏插入圖片描述
在這裏插入圖片描述

實現方法只需要在student類裏面編輯一個hashmap保存各個國家的值,然後在student-form.jsp裏面調用就行。

student類

	private LinkedHashMap<String,String> countryOptions;
	
	public Student() {
		// populate country options : used ISO country code
		countryOptions = new LinkedHashMap<>();
		
		countryOptions.put("BR","Brazil");  //(key,value)
		countryOptions.put("FR","France");
		countryOptions.put("DE","Gremany");
		countryOptions.put("IN","India");
		
		
	}
	
	public LinkedHashMap<String, String> getCountryOptions() {
		return countryOptions;
	}

student-form.jsp

		<form:select path="country">
		
			<form:options items="${student.countryOptions}" />

		</form:select>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章