SpringMvc表单标签库简单运用2(fm:checkbox,fm:radiobutton,fm:select)

学习目标:

前言:表单标签库的简单运用


代码:

main.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!-- 要使用Spring MVC提供的表单标签,首先需要在视图页面添加: -->
<%@ taglib prefix="fm" uri="http://www.springframework.org/tags/form" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'main.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <!-- modelAttribute属性指定该form绑定的是哪个Model,
  当指定了对应的Model后就可以在form标签内部其它表单标签
  上通过为path指定Model属性的名称来绑定Model中的数据了,
  method属性指定form的提交方式如GET、POST等。 -->
    <fm:form modelAttribute="user">
    <!-- 会生成一个type为text的Html input标签,通过path属性来指定要绑定的Model中的值。 -->
      姓名:<fm:input path="name"/><br><br>
      编号:<fm:input path="number"/><br><br>
      年龄:<fm:input path="age"/><br><br>
    爱好:<fm:checkbox path="hobbys" value="运动" label="运动运动"></fm:checkbox>
    <fm:checkbox path="hobbys" value="看书" label="看书看书"></fm:checkbox>
    <fm:checkbox path="hobbys" value="游戏" label="游戏游戏"></fm:checkbox><br><br>
    
        性别:<fm:radiobutton path="sex" value="男"/>男 &nbsp;
      <fm:radiobutton path="sex" value="女"/>女&nbsp;<br><br>
    
      学院:<fm:select path="dept">
      <fm:option value="1">信息与控制学院</fm:option>
       <fm:option value="2">生命学院</fm:option>
        <fm:option value="3">经管学院</fm:option>
         <fm:option value="4">学前教育学院</fm:option>
      </fm:select><br><br>
         </fm:form>
  </body>
</html>

SpringMvc表单标签库简单运用2(fm:checkbox,fm:radiobutton,fm:select)
model:

package model;

import java.util.List;

public class User {
	private String name;
	private int age;
	private String number;
	private List<String> hobbys;
	private String sex;
	private int dept;
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}
	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}
	/**
	 * @return the number
	 */
	public String getNumber() {
		return number;
	}
	/**
	 * @param number the number to set
	 */
	public void setNumber(String number) {
		this.number = number;
	}

	/**
	 * @return the hobbys
	 */
	public List<String> getHobbys() {
		return hobbys;
	}
	/**
	 * @param hobbys the hobbys to set
	 */
	public void setHobbys(List<String> hobbys) {
		this.hobbys = hobbys;
	}
	/**
	 * @return the sex
	 */
	public String getSex() {
		return sex;
	}
	/**
	 * @param sex the sex to set
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}
	/**
	 * @return the dept
	 */
	public int getDept() {
		return dept;
	}
	/**
	 * @param dept the dept to set
	 */
	public void setDept(int dept) {
		this.dept = dept;
	}
	
}

HelloController:

package controller;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.User;






import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value="/hello")
@SessionAttributes(value="user")
public class HelloController {
	//形参方式
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String register( String username, String pwd, String age,Model m){
		if(username.equals("1931030119")){
			
			//1创建一个对象:绑定数据用
			   User user = new User();
			   //2设置用户名
			   user.setName("lisi");
			   //3设置年龄
			   user.setAge(18);
			   //4设置编号
			   user.setNumber("A001");
				String a[]={"运动","看书","游戏"};
				List<String> list = new ArrayList();
				for(int i =0;i<3;i++){
				list.add(a[i]);
				}
				user.setHobbys(list);
				user.setSex("男");
				user.setDept(4);
				//设置user值,返回给结果页面
				m.addAttribute("user",user);
				//返回结果页面
				return "main";
		}else{
			return "index";
		}
	}

}

常见问题:

1.
SpringMvc表单标签库简单运用2(fm:checkbox,fm:radiobutton,fm:select)
乱码问题可以在web.xml设置过滤器解决,代码如下

	<filter>
		<filter-name>EncodingFilter</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>EncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

SpringMvc表单标签库简单运用2(fm:checkbox,fm:radiobutton,fm:select)
该问题可能因为其他标签未包含在fm:form中导致

SpringMvc表单标签库简单运用2(fm:checkbox,fm:radiobutton,fm:select)


上一篇:golang mgo的mongo连接池设置:必须手动加上maxPoolSize


下一篇:模拟FM的数字合成器