u12*_*123 4 eclipse jsp spring-mvc
我在 eclipse 中有以下 Spring 项目:
当我去:
http://[my-host]:8082/webapp-module/hello
Run Code Online (Sandbox Code Playgroud)
WEB /INF/jsp/hello.jsp页面加载得很好。但我还想在访问时定义一个默认起始页(WEB/INF/index.jsp):
http://[my-host]:8082/webapp-module
Run Code Online (Sandbox Code Playgroud)
目前这不起作用。我需要为此添加一个单独的控制器吗?
我的web.xml文件:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/webapp-module-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>webapp-module</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webapp-module</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
我的webapp-module-servlet.xml文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.samples" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
第 1 步:将 index.jsp 移至/WEB-INF/jsp/文件夹内。
第2步:在你的@Controller类中添加以下方法:
@RequestMapping("/")
public String home(){
return "index";
}
Run Code Online (Sandbox Code Playgroud)
完整的 Controller 类应该如下所示:
@Controller
public class LoginController {
@RequestMapping("/")
public String home(){
return "index";
}
@RequestMapping("/hello")
public String showhello(){
return "hello";
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10692 次 |
| 最近记录: |