我正在尝试做一个标准的spring mvc hello world应用程序,但是我想把控制器映射到根目录。(例如:http://numberformat.wordpress.com/2009/09/02/hello-world-spring-mvc-with-annotations/ ),所以唯一真正的区别是它们将其映射到host\appname \ So,而我希望将其映射到host\appname。
我将我的index.jsp放在src\main\webapp\jsp中,并在web.xml中将其映射为欢迎文件。我试过了:
@Controller("loginController")
public class LoginController{
@RequestMapping("/")
public String homepage2(ModelMap model, HttpServletRequest request, HttpServletResponse response){
System.out.println("blablabla2");
model.addAttribute("sigh", "lesigh");
return "index";
}
作为我的控制器,但我在tomcat的控制台中看不到任何内容。有人知道我把事情搞砸了吗?
我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- Index -->
<welcome-file-list>
<welcome-file>/jsp/index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>springweb</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springweb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
mvc-dispatcher-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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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="de.claude.test.*" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
我使用的是Spring3.0.5.release
或者这是不可能的,我是否需要将我的index.jsp放回web-inf的根目录中,并将重定向放到我的jsp中的某个地方,这样控制器就可以拿起它了?
发布于 2011-03-10 06:06:06
重定向是一种选择。您可以尝试的一件事是创建一个非常简单的索引页,将其放在WAR的根目录下,除了重定向到您的控制器之外什么也不做,如下所示
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:redirect url="/welcome.html"/>
然后将控制器与该URL进行映射,如下所示
@Controller("loginController")
@RequestMapping(value = "/welcome.html")
public class LoginController{
...
}
最后,在web.xml中,要使您的(新)索引可以访问,请声明
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
发布于 2014-04-21 14:03:00
我们可以简单地映射默认视图的Controller方法。例如,我们有一个index.html作为默认页面。
@RequestMapping(value = "/", method = GET)
public String index() {
return "index";
}
一旦完成,我们就可以使用默认的应用程序上下文访问页面了。
E.g http://localhost:8080/myapp
发布于 2013-03-10 17:21:01
它可以用更简单的方式解决:在web.xml中
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
在此之后,可以使用@RequestMapping(" index.htm ")处理任何需要处理index.htm的控制器。或者只使用索引控制器
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
<bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</bean>
https://stackoverflow.com/questions/5252065
复制相似问题