gpt4 book ai didi

java - Spring MVC 主页

转载 作者:行者123 更新时间:2023-12-02 02:44:04 26 4
gpt4 key购买 nike

我有 HomeController 类:

@Controller
public class HomeController {

@RequestMapping("/")
public String showPage() {
return "main-menu";
}
}

我的项目结构: enter image description here

Spring 版本:4.3.9web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>
<小时/>

它总是显示index.jsp,从不显示main-menu.jsp我想要一个我的主页的主菜单。我该怎么办?

最佳答案

我在我的项目中使用了一种方法来解决这个问题。下面与大家分享

仅在您的 index.jsp 中保留以下内容

index.jsp

<meta http-equiv="refresh" content="0;url=welcome" />

在您的 Controller java 程序中 RequestMapping必须保存 url 中指定的值<meta> 的属性index.jsp 中指定的标签文件。

在此示例中,url属性具有值 welcome

@Controller
public class HomeController {

@RequestMapping("/welcome")
public String welcome() {
return "main-menu";
}
}

这对我有用

更新#1:

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

更新#2:

<context:annotation-config />
<context:component-scan base-package="add your base folder here" />

关于java - Spring MVC 主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44933372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com