Java 使用 spring mvc 设置默认的 jsp 视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2511301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:39:09  来源:igfitidea点击:

Setting the default jsp view with spring mvc

javajspspring-mvc

提问by Walker

I want to set one of jsp files in my jsps folder as the default view for the application. Is there any way we can tell in <welcome-file-list>that abc.jspneeds to be default and it can be found in such and such path. Also the url pattern is html so is there a way it can be mapped in Spring MVC.

我想将我的 jsps 文件夹中的一个 jsp 文件设置为应用程序的默认视图。有什么方法可以告诉我们需要默认,并且可以在<welcome-file-list>某条abc.jsp路径中找到它。url 模式也是 html,所以有没有办法在 Spring MVC 中映射它。

For example - When a user types www.example.com, I want the application to direct to abc.jsppage and also when someone types www.example.com/something, even then application should direct to abc.jsp, but the url pattern shouldnt be compromised.

例如 - 当用户键入时www.example.com,我希望应用程序指向abc.jsp页面,并且当有人键入时www.example.com/something,即使应用程序也应该指向abc.jsp,但不应损害 url 模式。

采纳答案by Robby Pond

Add

添加

<mvc:view-controller path="/" view-name="abc"/>

to the config file. Then the ROOT will resolve to the abc view. Then add

到配置文件。然后 ROOT 将解析为 abc 视图。然后加

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

to the config file. This will resolve the view to /WEB-INF/view/abc.jsp.

到配置文件。这会将视图解析为 /WEB-INF/view/abc.jsp。

回答by BalusC

For example - When a user types www.example.com , I want the application to direct to abc.jsp page

例如 - 当用户输入 www.example.com 时,我希望应用程序定向到 abc.jsp 页面

Configure it as a <welcome-file>in the web.xmllike follows:

其配置为<welcome-file>web.xml类似如下:

<welcome-file-list>
    <welcome-file>/abc.jsp</welcome-file>
</welcome-file-list>

and also when someone types www.example.com/something, even then application should direct to abc.jsp, but the url pattern shouldnt be compromised.

并且当有人输入 www.example.com/something 时,即使这样,应用程序也应该直接指向 abc.jsp,但不应破坏 url 模式。

In other words, you'd like to forward non-existing resources (which thus would result in a HTTP 404 Page Not Found error) to the same file? Then define it as an <error-page>in the web.xmlas well:

换句话说,您想将不存在的资源(因此会导致 HTTP 404 Page Not Found 错误)转发到同一个文件?然后将其定义为<error-page>in web.xml

<error-page>
    <error-code>404</error-code>
    <location>/abc.jsp</location>
</error-page>

But your question is actually a bit ambiguous. If you actually didn't mean the above and actually want to use /abc.jspas the "page controller", then you need to define it as a <servlet>in web.xmlinstead:

但是你的问题其实有点模棱两可。如果您实际上不是上面的意思并且实际上想/abc.jsp用作“页面控制器”,那么您需要将其定义为 a <servlet>in web.xml

<servlet>
    <servlet-name>controller</servlet-name>
    <jsp-file>/abc.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>controller</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

(both the <welcome-file>and the <error-page>are not needed here)

(这里不需要<welcome-file><error-page>

This is however a flaw in the MVC design (using a view as controller). But if you're actually asking for it..

然而,这是 MVC 设计中的一个缺陷(使用视图作为控制器)。但如果你真的要求它..

回答by Walker

Solved -

解决了 -

A file in the welcome file list has to be a real file , hence abc.htm did not work. The only way to accomplish was to create an index.jsp and redirect it using

欢迎文件列表中的文件必须是真实文件,因此 abc.htm 不起作用。完成的唯一方法是创建一个 index.jsp 并使用重定向它

<jsp:forward page="abc.html" />

also in yourapplication-servlet.xml you need to specify the bean mapping like

同样在 yourapplication-servlet.xml 中,您需要指定 bean 映射,如

<bean name="/abc.html" class="package.mypack.someController">  
    <property name="commandClass" value="package.mypack..something"/>
    <property name="formView" value="abc"/>
    <property name="successView" value="abc.htm"/>
</bean>  

A welcome-file has to be a REAL file on the file system it doesn't work with mapped urls. You can create an index.jsp which redirects to the mapped url, but it has to be an actual file. Nothing to do with spring it is stated in the servlet spec that it has to be this way, although behavior might vary on different app servers in general it has to be a real file.

欢迎文件必须是文件系统上的真实文件,它不适用于映射的 url。您可以创建一个重定向到映射 url 的 index.jsp,但它必须是一个实际文件。与 spring 无关,它在 servlet 规范中声明必须是这种方式,尽管行为在不同的应用服务器上可能会有所不同,但通常它必须是一个真实的文件。

I hope this help someone

我希望这有助于某人

回答by ylev

This all behavior can be resolved by adding

这所有的行为都可以通过添加来解决

<mvc:default-servlet-handler>

to your spring ../WEB-INF/dispatcher-servlet.xml
This will arrange it all for you.

到你的春天../WEB-INF/dispatcher-servlet.xml
这一切都会为你安排好。