spring应用程序的默认html页面

时间:2013-11-05 17:32:57

标签: java spring-mvc web-applications

我有一个带url的web应用程序:localhost:8080 / appname / views / welcome.html。因此,我希望我的应用程序在用户输入localhost:8080 / appname时打开welcome.html,而不是输入整个url。所以我在spring config xml中做了以下更改。

<mvc:annotation-driven />
<mvc:resources mapping="/static/**" location="/" />
<mvc:view-controller path="/" view-name="welcome" />
<mvc:default-servlet-handler />
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/views/" />
    <property name="suffix" value=".html" />
</bean>

但是使用此配置,我无法在浏览器中启动应用程序,而是出现404错误。

需要帮助我做错了。

2 个答案:

答案 0 :(得分:1)

您可以通过在web.xml中添加以下内容来实现。

<welcome-file-list>
    <welcome-file>/views/welcome.html</welcome-file>
</welcome-file-list>

答案 1 :(得分:1)

您还可以创建Spring操作并使用@RequestMapping中的value属性来指定要调用的url或调用该操作。调用该操作时,您将返回页面的名称,在本例中为welcome.html。因此,您可以使用此方法通过告知要映射到“/”的操作来加载welcome.html页面。

@Controller
public class CustomController {

@RequestMapping (value = {"/","home"}, method = RequestMethod.GET)
public String home ()
{
    return "welcome";
}
}

so localhost:8080 / app-name / 和
   本地主机:8080 /应用程序名/家