Spring Annotations

在这个例子将会学习怎样使用spring annotations填充一个表单。带annotation的Controller类如下:

package com.zcl.spring.formtag;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
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.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@RequestMapping("/userRegistration.html")
@SessionAttributes("user")
public class UserController{
	
	private UserService userService ;

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	@ModelAttribute("countryList")
	public List<Country> populateCountryList(){
		return userService.getAllCountries() ;
	}
	
	@ModelAttribute("communityList")
	public List<Community> populateCommunityList(){
		return userService.getAllCommunities() ;
	}
	@RequestMapping(method = RequestMethod.GET)
	public String showUserForm(ModelMap model){
		User user = new User() ;
		model.addAttribute("user", user);
		return "userForm" ;
	}
	
	public String onSubmit(@ModelAttribute("user")User user){
		userService.add(user) ;
		return "redirect:userSuccess.html" ;
	}
}

populateCountryList() 和 populateCommunityList()方法分别用来填充country和community,@ModelAttribute annotation用于方法上时表示包含着模型层的应用数据。所以它会在表单加载前被调用。这和覆写继承SimpleFormController的referenceData()相似。用于最后使用了重定向到UserSuccess.html所以我们还需要一个UserSuccessController。

package com.zcl.spring.formtag;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserSuccessController {
	
	@RequestMapping("/userSuccess.html")
	public String redirect(){
		return "userSuccess" ;
	}
}
在重新配置一下dispather-servlet.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans 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"
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans"> 
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/jsp/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
	<bean id="userService" class="com.zcl.spring.formtag.UserServiceImpl" />
	<context:component-scan base-package="com.zcl.spring.formtag" />
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
</beans>
其他地方基本和我们前面做的差不多。运行结果一致。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值