我有一个关于如何在这个特定的Spring MVC配置中实现路由的问题。
因此,在名为 mvc-config.xml 的文件中,我有以下MVC配置:
<!-- other option is UrlFilenameViewController -->
<mvc:view-controller path="/" view-name="/index"/>
<mvc:view-controller path="/index.html" view-name="/index"/>
<mvc:view-controller path="/html5.html" view-name="/html5/html5"/>
<mvc:view-controller path="/about.html" view-name="/about"/>
<mvc:view-controller path="/admin/admin.html" view-name="/admin/admin"/>
<mvc:view-controller path="/login.html" view-name="/form/login"/>
<mvc:view-controller path="/denied.html" view-name="/error/denied"/>
<mvc:view-controller path="/rest.html" view-name="/rest/rest"/>
<mvc:view-controller path="/file.html" view-name="/file/file"/> <!-- Mapping per l'empio dell'upload file -->
<mvc:view-controller path="/cookieView.html" view-name="/cookie/cookieView"/>
<mvc:view-controller path="/jstl.html" view-name="/jstl/jstl"/>
<mvc:view-controller path="/audiovideo.html" view-name="/audiovideo/audiovideo"/>
<mvc:view-controller path="/jdbc.html" view-name="/jdbc/jdbc"/>
<mvc:view-controller path="/orm.html" view-name="/orm/orm"/>
<mvc:view-controller path="/scope.html" view-name="/scope/scope"/>
<mvc:view-controller path="/maintenance.html" view-name="/maintenance"/>
<mvc:view-controller path="/security.html" view-name="/security/security"/>
<mvc:view-controller path="/controller.html" view-name="/controller/controllerView"/>
我从未见过这种设置,而且我总是使用控制器类(带注释的con @Controler ),其中包含使用 @RequestMapping 注释的方法,这些方法可以处理特定的HttpRequest。资源并返回逻辑视图名称。
因此,在我工作的应用程序中,似乎MVC逻辑非常不同。
在我看来,我没有控制器类,并且路由是明确处理的。
例如,我有:
<mvc:view-controller path="/file.html" view-name="/file/file"/>
我认为这意味着:处理 /file.html 资源的所有请求,显示 / file / <中的 file.jsp 页面/ strong>视图目录的文件夹。
事实上,在view目录中我有 /file/file.jsp 页面:
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="page" tagdir="/WEB-INF/tags" %>
<page:template>
<jsp:attribute name="title">
Spring MVC File Upload Controller Example
</jsp:attribute>
<jsp:body>
<c:url value="/uploadFile" var="fileUploadControllerURL" />
<h1><b>Spring MVC FileUploadController Example</b></h1> <br/>
<form action="${fileUploadControllerURL}" method="post" enctype="multipart/form-data">
<table>
<tr><td><b>File:</b></td><td><input type="file" name="file"></td><td><input type="submit" value="Press to upload the File"></td></tr>
</table>
</form>
<br/><br/>
<c:url value="/excel" var="excelController" />
<a href="${excelController}">Excel</a><br/>
<c:url value="/pdf" var="PDFController" />
<a href="${PDFController}">PDF</a>
</jsp:body>
</page:template>
所以,正如你所看到的,在这个页面上我有类似的东西:
<c:url value="/excel" var="excelController" />
<a href="${excelController}">Excel</a><br/>
究竟意味着什么?
在我看来,第一行将 / excel 值放在名为 excelController 的变量中,然后将其用于 href
我的推理是正确还是我错过了什么?
答案 0 :(得分:1)
<c:url>
是一个非常有用且功能强大的jspl-tag。一般来说你是对的:
<c:url value="/excel" var="excelController" />
<a href="${excelController}">Excel</a><br/>
在我看来,第一行将/ excel值放在名为excelController的变量中,然后将其用于href
但是缺少一些细节:excelController
中存储的值并不完全是/excel
,而是/excel
的域相对网址,它可能会有一个sessionId参数(取决于您的会话跟踪配置方式。)
例如,假设您使用此应用程序运行localhost tomcat,名为&#34; MyApp&#34;并使用Cookie跟踪会话,然后excelController
将是:/MyApp/excel