Spring MVC基礎之表單標籤庫
表單標籤庫涉及到到的標籤
0、文本框和密碼框
1、文本域
2、複選框
3、多項複選框
3、單選按鈕
4、多項單選按鈕
5、下拉框
6、列表多選項
7、隱藏字段域
8、錯誤處理
9、文件上傳處理
使用Spring MVC創建一個項目如下:
編寫如下代碼:
Student.java
package my.model;
public class Student {
private String name;
private String sex;
private String id;
private int age;
private String grade;
private String school;
private String email;
private String hobby;
private String advantage;
private String education;
//構造方法:自己補全
//setter和getter方法:自己補全
//toString()方法自己補全
}
StudentController.java
package my.controller;
import my.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
@Controller
public class StudentController {
//定位到首頁:index.jsp
@RequestMapping(value = "/index.do")
public ModelAndView index(@ModelAttribute("student") Student student){
return new ModelAndView("index","student",new Student());
}
//定位到結果打印頁面:
@RequestMapping(value = "/result.do")
public String result(@ModelAttribute("student") Student student, ModelMap modelMap){
modelMap.addAttribute("name",student.getName());
modelMap.addAttribute("age",student.getAge());
modelMap.addAttribute("id",student.getId());
modelMap.addAttribute("sex",student.getSex());
modelMap.addAttribute("grade",student.getGrade());
modelMap.addAttribute("school",student.getSchool());
modelMap.addAttribute("email",student.getEmail());
modelMap.addAttribute("hobby",student.getHobby());
modelMap.addAttribute("advantage",student.getAdvantage());
modelMap.addAttribute("education",student.getEducation());
return "/pages/result";
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_4_0.xsd"
version="4.0">
<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>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--編碼過濾器,防止出現亂碼-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
dispatcher-servlet.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="my.controller"/>
<!--映射到動態頁面-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
index.jsp
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%
//定義一個list_grade保存多個年級選項列表
List<String> list_grade = new ArrayList<String>();
list_grade.add("大一");
list_grade.add("大二");
list_grade.add("大三");
list_grade.add("大四");
//定義一個list_grade保存多個愛好的選項列表
List<String> list_hobby = new ArrayList<String>();
list_hobby.add("籃球");
list_hobby.add("足球");
list_hobby.add("羽毛球");
list_hobby.add("鉛球");
%>
<%--@elvariable id="student" type="my.model.Student"--%>
<form:form action="/day0830/result.do" commandName="student" modelAttribute="student">
姓名:<form:input path="name"/>
性別:<form:radiobutton path="sex" value="男" label="男" /> <form:radiobutton path="sex" value="女" label="女" />
學號:<form:input path="id"/>
年齡:<form:select path="age">
<form:option value="0" label="請選擇..." />
<form:option value="15" label="15" />
<form:option value="20" label="20" />
<form:option value="30" label="30" />
<form:option value="40" label="40" />
<%--如果有一個集合返回使用:<form:options items="${ageList}" />--%>
</form:select>
年級:<form:select path="grade" items="<%=list_grade%>" multiple="true"/>
學校:<form:input path="school"/>
郵箱:<form:input path="email"/>
愛好:<form:checkboxes path="hobby" items="<%=list_hobby%>" /> <%--如果有很多項使用:<form:checkboxs path="" value="">--%>
<%--特長爲隱藏字段--%>
<form:hidden path="advantage" value="暫無特長"/>
教育經歷:<form:textarea path="education" cols="30" rows="5"/>
</form:form>
</body>
</html>
result.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h4>學生信息如下:</h4>
<hr>
姓名:${name}
年齡:${age}
性別:${sex}
學號:${id}
年級:${grade}
學校:${school}
郵箱:${email}
愛好:${hobby}
特長:${advantage}
教育經歷:${education}
</body>
</html>
運行:
點擊提交:
未完待續…
後續預告:標籤庫-錯誤處理&文件上傳處理