Hug*_*ugo 60 spring spring-mvc
我正在尝试使用其中一个标准的spring mvc hello world应用程序,但我想将控制器映射到根目录.(例如:http://numberformat.wordpress.com/2009/09/02/hello-world-spring-mvc-with-annotations/)所以唯一真正的区别是他们将它映射到host\appname\something和我想将它映射到主机\ 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";
}
Run Code Online (Sandbox Code Playgroud)
作为我的控制器,但我看到我的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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我正在使用Spring 3.0.5.release
或者这是不可能的,我是否需要将我的index.jsp放回web-inf的根目录并将重定向放到我的jsp中的某个位置,以便控制器选择它?
Mik*_*Lin 71
即使遵循Sinhue的设置,我也遇到了同样的问题,但我解决了它.
问题是当我在WebContent目录中有文件index.jsp时,某些东西(Tomcat?)从"/"转发到"/index.jsp".当我删除它时,请求不再转发.
我为诊断问题所做的是创建一个catch-all请求处理程序并将servlet路径打印到控制台.这告诉我,即使我正在进行的请求是http:// localhost/myapp /,servlet路径也被更改为"/index.html".我原以为它是"/".
@RequestMapping("*")
public String hello(HttpServletRequest request) {
System.out.println(request.getServletPath());
return "hello";
}
Run Code Online (Sandbox Code Playgroud)
总而言之,您需要遵循的步骤是:
<url-pattern>/</url-pattern>RequestMapping("/")希望这可以帮助.
Emm*_*ini 18
重定向是一种选择.你可以尝试的一件事是创建一个非常简单的索引页面,放在WAR的根目录下,除了重定向到你的控制器之外什么都不做
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:redirect url="/welcome.html"/>
Run Code Online (Sandbox Code Playgroud)
然后使用类似的方法将控制器映射到该URL
@Controller("loginController")
@RequestMapping(value = "/welcome.html")
public class LoginController{
...
}
Run Code Online (Sandbox Code Playgroud)
最后,在web.xml中,要使您的(新)索引JSP可访问,请声明
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
Run Code Online (Sandbox Code Playgroud)
Sat*_*jan 12
我们可以简单地为默认视图映射Controller方法.例如,我们有一个index.html作为默认页面.
@RequestMapping(value = "/", method = GET)
public String index() {
return "index";
}
Run Code Online (Sandbox Code Playgroud)
完成后,我们可以使用默认的应用程序上下文访问页面
E.g http://localhost:8080/myapp
Run Code Online (Sandbox Code Playgroud)
它对我有用,但有些不同:
我知道这些差别不大,但我很确定(我现在不工作)这是我的配置,它适用于Spring MVC 3.0.5.
还有一件事.您没有在web.xml中显示调度程序配置,但也许您有一些预先添加.它必须是这样的:
<servlet-mapping>
<servlet-name>myServletName</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
如果不是这种情况,您需要一个url-rewrite过滤器或尝试重定向解决方案.
编辑:回答你的问题,我的视图解析器配置也有一点不同:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
146564 次 |
| 最近记录: |