Spring MVC文本框示例

在Spring MVC中,可以使用<form:input />标记来呈现HTML文本框字段。 例如,

<form:input path="userName" />

它将呈现以下HTML代码

<input id="userName" name="userName" type="text" value=""/>

在本教程中,我们向您展示如何使用Spring的表单标签“ input ”来呈现一个HTML文本框来存储“ userName ”。 此外,添加一个空的检查验证器以确保文本框值不为空。

1.控制器

一个SimpleFormController来处理表单值,并将表单值链接到Customer对象。

文件:TextBoxController.java

package com.mkyong.customer.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import com.mkyong.customer.model.Customer;

public class TextBoxController extends SimpleFormController{
	
	public TextBoxController(){
		setCommandClass(Customer.class);
		setCommandName("customerForm");
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
		HttpServletResponse response, Object command, BindException errors)
		throws Exception {

		Customer customer = (Customer)command;
		return new ModelAndView("CustomerSuccess","customer",customer);
	
	}
}

2.型号

一个Customer对象,用于存储文本框值。

文件:Customer.java

package com.mkyong.customer.model;

public class Customer{
	
	String userName;
	//getter and setter methods
}

3.表单验证器

创建一个表单验证器类,并使用ValidationUtils类确保“ userName”不为空,否则,从相应的资源包(属性文件)中获取“ required.userName ”消息。

文件:CustomerValidator.java

package com.mkyong.customer.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.mkyong.customer.model.Customer;

public class CustomerValidator implements Validator{

	@Override
	public boolean supports(Class clazz) {
		//just validate the Customer instances
		return Customer.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName",
			"required.userName", "Field name is required.");
		
	}
	
}

文件:message.properties

required.userName = username is required!

4.查看

一个JSP页面,使用Spring的表单标签“ input ”来呈现HTML文本框,并放置一些CSS样式以突出显示错误消息。

文件:CustomerForm.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<style>
.error {
	color: #ff0000;
}

.errorblock {
	color: #000;
	background-color: #ffEEEE;
	border: 3px solid #ff0000;
	padding: 8px;
	margin: 16px;
}
</style>
</head>

<body>
	<h2>Spring's form textbox example</h2>

	<form:form method="POST" commandName="customerForm">
		<form:errors path="*" cssClass="errorblock" element="div" />
		<table>
			<tr>
				<td>Username :</td>
				<td><form:input path="userName" />
				</td>
				<td><form:errors path="userName" cssClass="error" />
				</td>
			</tr>
			<tr>
				<td colspan="3"><input type="submit" />
				</td>
			</tr>
		</table>
	</form:form>

</body>
</html>

如果提交了表单,则渲染成功的页面并显示提交的文本框值。

文件:CustomerSuccess.jsp

<html>
<body>
	<h2>Spring's form textbox example</h2>

	userName : ${customer.userName}

</body>
</html>

5. Spring Bean配置

全部链接〜

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <bean
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

	<bean class="com.mkyong.customer.controller.TextBoxController">
		<property name="formView" value="CustomerForm" />
		<property name="successView" value="CustomerSuccess" />

		<!-- Map a validator -->
		<property name="validator">
			<bean class="com.mkyong.customer.validator.CustomerValidator" />
		</property>
	</bean>

	<!-- Register the Customer.properties -->
	<bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basename" value="message" />
	</bean>

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/pages/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>

</beans>

6.演示

访问页面– http:// localhost:8080 / SpringMVCForm / textbox.htm

SpringMVC-TextBox-Example-1

如果提交表单时文本框值为空,则显示并突出显示错误消息。

SpringMVC-TextBox-Example-2

如果表单提交成功,则只显示提交的文本框值。

SpringMVC-TextBox-Example-3

下载源代码

下载它– SpringMVCForm-TextBox-Example.zip (9KB)

翻译自: https://mkyong.com/spring-mvc/spring-mvc-textbox-example/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值