问题:我想单击链接createExcel并下载由JAVA Code创建的MS.Excel,但是当我单击此链接时,直接转到excelView1.jsp页面并显示错误404页面。这是我从AbstractController.class扩展的控制器

@Controller
public class HomeController extends AbstractController{
@Override
@RequestMapping(value = "/createExcel", method = RequestMethod.GET)
protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    return new ModelAndView("excelView1");
}
}


我想通过id = excelView1重定向到XML,不转到jsp目录,这是使用XmlViewResolver处理的root-context.xml。

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean class="com.gfam.kh.HomeController"/>
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
    <property name="location">
        <value>
             classpath:create-excel.xml
        </value>
    </property>
</bean>


这是create-excel.xml上的代码

<bean id="excelView1"
    class="com.gfam.kh.model">
</bean>


这是ExcelBuilder上的代码

public class ExcelBuilder extends AbstractExcelView{
@Override
protected void buildExcelDocument(Map<String, Object> datas,
        HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse reponse)
        throws Exception {
    System.out.println("Hello MS.Excel");
    HSSFSheet sheet1 = workbook.createSheet("sheetname");
}
}

最佳答案

我只是解释了XmlViewResolver的工作方式,对您有帮助,请尝试一下。

(1)Views.xml

<bean class="org.springframework.web.servlet.view.XmlViewResolver">
    <property name="location">
        <value>/WEB-INF/example-views.xml</value>
    </property>
</bean>

(2)example-views.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="home" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/jsp/home.jsp" />
    </bean>

    <bean id="admin/home" class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/jsp/admin/home.jsp" />
    </bean>

    <bean id="logOffRedirect" class="org.springframework.web.servlet.view.RedirectView">
        <property name="url" value="home" />
    </bean>
</beans>

09-15 22:15