通过适当的例子了解spring mvc中的BeanNameViewResolver

时间:2017-06-18 11:37:13

标签: java spring spring-mvc spring-annotations

我是Spring的新手,我想知道spring mvc中的BeanNameViewResolver。 我试过一个例子。这个例子不起作用。 我无法理解这个问题。 问题是我的请求没有给控制器。

源代码: web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>springmvcex1_InternalResource</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.form</url-pattern>
  </servlet-mapping>
</web-app>

弹簧servlet.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
 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-4.1.xsd">    
    <context:component-scan base-package="com.example" />

   <bean id="hm" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
    </bean>


    <bean id="viewResolver" 
 class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

    <bean id="myExcelView" class="com.example.CustomExcelView" />

</beans>

ExcelController.java:

@Controller
@RequestMapping("/excelview")
public class ExcelController {
    public String getExcel(Model model){
        Map<String,String> revenueData = new HashMap<String,String>();
        revenueData.put("Jan-2010", "$100,000,000");
        revenueData.put("Feb-2010", "$110,000,000");
        revenueData.put("Mar-2010", "$130,000,000");
        revenueData.put("Apr-2010", "$140,000,000");
        revenueData.put("May-2010", "$200,000,000");
        model.addAttribute("revenueData",revenueData);

        return "myExcelView";
    }
}

CustomExcelView.java:

public class CustomExcelView 
{
    protected void buildExcelDocument(Map model)
            throws Exception {
            Map<String,String> map = (Map<String,String>) model[0][1]][1].get("revenueData");
            map.put("hi", "hi");



    }
}

request.jsp 点击

0 个答案:

没有答案