Part 78 - MVC中不同的ActionResult 类型

In this section, we will discuss different types of ActionResult objects that can be returned by an action method.

The following is the signature of a typical action method in a controller. Notice that, the return type is ActionResult. ActionResult is an abstract class and has several sub types(子类).

public ActionResult Index()
{
    return View();
}
Here is the list of all sub-types of ActionResult.


I have used a tool called ILSpy, to list all the sub-types of ActionResult. To use the tool yourself, here are the steps
1. Navigate to http://ilspy.net
2. Click on "Download Binaries" button, and extract them to a folder.
3. Run ILSpy.exe which can be found in the folder, where you have extracted the binaries.
4. Click on File - Open From GAC
5. Type "System.Web.Mvc" in the search box. Select the Assembly and click Open
6. At this point System.Web.Mvc assmbly should be loaded into ILSpy. Expand System.Web.Mvc, then expand ActionResult and then expand "Derived Types". You should now be able to see all the derived types.

Why do we have so many sub-types?
An action method in a controller, can return a wide range of objects. For example, an action method can return
1. ViewResult
2. PartialViewResult
3. JsonResult
4. RedirectResult etc..

What should be the return type of an action method - ActionResult or specific derived type? 
It's a good practise to return specific sub-types, but, if different paths of the action method returns different subtypes, then I would return an ActionResult object. An example is shown below.

public ActionResult Index()
{
    if (Your_Condition)
        return View();            // returns ViewResult object
    else
        return Json("Data");  // returns JsonResult object
}



The following table lists 
1. Action Result Sub Types
2. The purpose of each sub-type
3. The helper methods used to retrun the specific sub-type












### 回答1: 以下是一个简单的 spring-mvc.xml 文件示例: ``` <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.example.controller"/> <mvc:annotation-driven /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans> ``` 请注意,上面的文件只是一个示例,您可以根据自己的需求进行修改和扩展。 ### 回答2: 生成一个简单的spring-mvc.xml文件需要以下几个步骤: 1. 首先,在文件的开头添加xml的声明和命名空间。声明如下: ``` <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> ``` 2. 接下来,配置mvc的注解驱动。在文件添加如下代码: ``` <mvc:annotation-driven /> ``` 这样spring将会自动注册默认的一些处理器和适配器,用来支持基于注解的请求映射和请求参数的解析和绑定。 3. 然后,配置视图解析器,用来解析Controller方法返回的视图名。可以通过以下代码添加一个InternalResourceViewResolver视图解析器: ``` <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> ``` 上述配置将会将Controller方法返回的视图名加上前缀"/WEB-INF/views/"和后缀".jsp",最终形成完整的JSP视图路径。 4. 配置组件扫描,以便自动扫描和注册Controller,Service,Repository等组件。可以通过以下代码配置组件扫描: ``` <context:component-scan base-package="com.example.controller" /> <context:component-scan base-package="com.example.service" /> <context:component-scan base-package="com.example.repository" /> ``` 上述代码将分别扫描并注册com.example.controller,com.example.service和com.example.repository下的组件。 5. 最后,添加xml文件的结尾标签 "</beans>" 结束配置。 通过以上步骤,就能生成一个简单的spring-mvc.xml文件,用于配置Spring MVC的基本设置和组件的注册。 ### 回答3: 这是一个简单的spring-mvc.xml文件的示例: ``` <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven/> <mvc:resources mapping="/resources/**" location="/resources/"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="messages"/> </bean> <bean id="myController" class="com.example.MyController"></bean> <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="viewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10000000"/> </bean> </beans> ``` 这个配置文件,主要包含以下配置: 1. `<mvc:annotation-driven/>` 这个配置用于启用Spring MVC的注解驱动。 2. `<mvc:resources>` 用于配置静态资源的处理器,比如js、css等文件。 3. `<bean id="viewResolver">` 配置了一个InternalResourceViewResolver视图解析器,用于将逻辑视图名对应的JSP文件解析为实际的物理视图。 4. `<bean id="messageSource">` 配置了一个ResourceBundleMessageSource用于国际化处理,它可以加载不同语言的消息资源文件。 5. `<bean id="handlerMapping">` 配置了一个BeanNameUrlHandlerMapping,用于将请求的URL映射到对应的处理器。 6. `<bean id="viewController">` 配置了一个UrlFilenameViewController,用于将URL的文件名作为逻辑视图名进行解析。 7. `<bean id="multipartResolver">` 配置了一个CommonsMultipartResolver,用于处理文件上传的请求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值