Spring MVC之SimpleFormController的简单使用

很明显,SimpleFormController是处理表单提交比较好的一种控制器。下面就一个小例子对SimpleFormController的用法做一个总结。

首先提纲挈领,我们的例子是做一个新闻提交模块。

如下图所示:

Spring MVC之SimpleFormController的简单使用_第1张图片

在输入http://localhost:8080/javalearn1.0/addNews.html后,即打开此页面,从数据库中读出新闻类别并在select框中显示。而用户填完新闻标题和新闻内容后,即保存至数据库,并刷新页面,在提交新闻按钮下方显示:保存数据成功。

第一步的显示对应一个ShowNewsClassController,而第二步保存对应AddNewsController。

首先是javalearn-servlet.xml的配置:

<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  	<property name="mappings">
  	    <props>
    		<prop key="/addNews.html">showNewsClassController</prop>
  		<prop key="/addNews1.html">addNewsController</prop>
  		</props>
  	</property>
</bean>

对应的ShowNewsClassController为:

package com.javalearn.control;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import com.javalearn.service.*;
import java.util.*;
import com.javalearn.model.*;
public class ShowNewsClassController extends AbstractController{

	private JavalearnService javalearnService;
	
	public JavalearnService getJavalearnService() {
		return javalearnService;
	}

	public void setJavalearnService(JavalearnService javalearnService) {
		this.javalearnService = javalearnService;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		
		List<Tbclass> list=this.javalearnService.readNewsClass();
		Map<String,List<Tbclass>> map=new HashMap<String,List<Tbclass>>();
		map.put("myclass",list);
		
		return new ModelAndView("addNews","map",map);
	}	
}

这个类就是在页面加载时,读取新闻类别并进行显示。

对应的addNews.jsp页面为:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>欢迎访问Javalearn JAVA学习网</title>
</head>
<body>
<form action="addNews1.html" method="post">
<p>新闻类别:
<select name="myclass">
 <c:forEach items="${map.myclass}" var="Parameter">
   <option value="${Parameter.id}">${Parameter.classname }</option>
  </c:forEach>
</select></p>
<p>新闻标题:<input name="title" type="text"/></p>
<p>新闻内容:<textarea name="mycontent" rows="10" cols="100"></textarea></p>
<p><input type="submit"  value="提交新闻"/></p>
</form>
<font color="red">${map.message}</font>
</body>
</html>

即,点击了提交按钮后,跳转至addNews1.html。而由在javalearn-servlet.xml中配置可知,此url交由AddNewsController进行处理。AddNewsController继承至SimpleFormController,onSubmit方法处理提交事件。且form中的各个控件的name对应AddNewsController的CommandClass的属性。类似于Struts2或JavaBean的用法。

package com.javalearn.control;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.javalearn.model.Tbclass;
import com.javalearn.model.Tbnews;
import com.javalearn.service.*;
import java.util.*;
/*
 * author:Tammy Pi
 * function:添加新闻的Controller
 */
public class AddNewsController extends  SimpleFormController{

	private JavalearnService javalearnService;
	public AddNewsController()
	{
		this.setCommandClass(Tbnews.class);
		this.setCommandName("Tbnews");
	}
	public JavalearnService getJavalearnService() {
		return javalearnService;
	}
	public void setJavalearnService(JavalearnService javalearnService) {
		this.javalearnService = javalearnService;
	}
	
	protected ModelAndView onSubmit(Object command,BindException bindException) throws Exception
	{
		Tbnews tbnews=(Tbnews) command;
		
		Map map=new HashMap<String,String>();
		List<Tbclass> list=this.javalearnService.readNewsClass();
		if(this.javalearnService.saveNews(tbnews))
		{
			map.put("message","保存数据成功!");
		}
		else
		{
			map.put("message","保存数据失败!");
		}
		map.put("myclass",list);
		return new ModelAndView(this.getSuccessView(),"map",map);
	}
}

你可能感兴趣的:(spring,exception,mvc,list,Class,import)