spring web框架 2

spring web框架


5.Views and resolving them


The two interfaces which are important to the way Spring handles views are ViewResolver and View. The ViewResolver provides a mapping between view names and actual views. The View interface addresses the preparation of the request and hands the request over to one of the view technologies. ViewResolve的主要作用是将view names映射到实际的view中。

5.1 ViewResolve接口实现类的使用方法

all controllers in the Spring Web MVC framework return a ModelAndView instance. Views in Spring are addressed by a view name and are resolved by a view resolver.下面是一些简单的实现实现类。

ViewResolver Description
AbstractCachingViewResolver An abstract view resolver which takes care of caching views. Often views need preparation before they can be used, extending this view resolver provides caching of views.
XmlViewResolver An implementation of  ViewResolver   that accepts a configuration file written in XML with the same DTD as Spring's XML bean factories. The default configuration file is  /WEB-INF/views.xml .
ResourceBundleViewResolver An implementation of  ViewResolver   that uses bean definitions in a  ResourceBundle , specified by the bundle basename. The bundle is typically defined in a properties file, located in the classpath. The default file name is  views.properties .
UrlBasedViewResolver A simple implementation of the  ViewResolver   interface that effects the direct resolution of symbolic view names to URLs, without an explicit mapping definition. This is appropriate if your symbolic names match the names of your view resources in a straightforward manner, without the need for arbitrary mappings.
InternalResourceViewResolver A convenience subclass of  UrlBasedViewResolver   that supports  InternalResourceView   (i.e. Servlets and JSPs), and subclasses such as  JstlView   and  TilesView . The view class for all views generated by this resolver can be specified via  setViewClass(..) . See the Javadocs for the  UrlBasedViewResolver   class for details.
VelocityViewResolver   /FreeMarkerViewResolver A convenience subclass of  UrlBasedViewResolver   that supports  VelocityView   (i.e. Velocity templates) or  FreeMarkerView   respectively and custom subclasses of them.

使用的一个简单的demo, UrlBasedViewResolver

<bean id="viewResolver"

class="org.springframework.web.servlet.view.UrlBasedViewResolver">

<property name="prefix" value="/WEB-INF/jsp/"/>

<property name="suffix" value=".jsp"/>

</bean>

When returning  test   as a viewname, this view resolver will hand the request over to the  RequestDispatcher   that will send the request to/WEB-INF/jsp/test.jsp .

<bean id="viewResolver"

class="org.springframework.web.servlet.view.ResourceBundleViewResolver">

<property name="basename" value="views"/>

<property name="defaultParentView" value="parentView"/>

</bean>


5.2 ViewResolvers

spring支持使用 ViewResolvers 链来处理web中的请求。 If a specific view resolver does not result in a view, Spring will inspect the context to see if other view resolvers are configured. If there are additional view resolvers, it will continue to inspect them. If not, it will throw an  Exception .

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.
InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="excelViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="order" value="1"/>
<property name="location" value="/WEB-INF/views.xml"/>
</bean>
<!-- in views.xml
-->

<beans>
<bean name="report" class="org.springframework.example.ReportExcelView"/>
</beans>


6.Spring's multipart (fileupload) support(文件上传的支持)

这里是一个简单的在spring框架下文件上传的例子。spring提供了MultipartResolvers来支持Commons FileUpload和Cos FileUpload。

6.1添加下载到jar(FileUpload,Cos FileUpload)文件到工程中。

6.2设置bean

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>

<bean id="multipartResolver" class="org.springframework.web.multipart.cos.CosMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>

6.3在页面中承载上传文件对话框

<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>Please upload a file</h1>
<form method="post" action="upload.form" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>

6.4 So, to be able to upload files using a (HTML) form, declare the resolver, a url mapping to a controller that will process the bean, and the controller itself.

<beans>
<!-- lets use the Commons-based implementation of the MultipartResolver interface -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/upload.form=fileUploadController
</value>
</property>
</bean>
<bean id="fileUploadController" class="examples.FileUploadController">
<property name="commandClass" value="examples.FileUploadBean"/>
<property name="formView" value="fileuploadform"/>
<property name="successView" value="confirmation"/>
</bean>
</beans>

6.5创建controller类

public class FileUploadController extends SimpleFormController {
protected ModelAndView onSubmit(
HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors) throws ServletException, IOException {
// cast the bean
FileUploadBean bean = (FileUploadBean) command;
let's see if there's content there
String file = bean.getFile();
if (file == null) {
// hmm, that's strange, the user did not upload anything
}
// well, let's do nothing with the bean for now and return
return super.onSubmit(request, response, command, errors);
}
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)
throws ServletException {
// to actually be able to convert Multipart instance to a String
// we have to register a custom editor
binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
// now Spring knows how to handle multipart object and convert them
}
}
public class FileUploadBean {
private String file;

public void setFile(String file) {
this.file = file;
}
public String getFile() {
return file;
}
}

As you can see, the  FileUploadBean   has a property typed  byte[]   that holds the file. The controller registers a custom editor to let Spring know how to actually convert the multipart objects the resolver has found to properties specified by the bean. Of course, this last example only makes (logical) sense in the context of uploading a plain text file (it wouldn't work so well in the case of uploading an image file).







posted @ 2010-11-10 21:56  qiang.xu  阅读(1816)  评论(0编辑  收藏  举报