【问题标题】:Spring 3 MVC: Java Based Config using servlet container 2.5 throwing 404Spring 3 MVC:使用 servlet 容器 2.5 抛出 404 的基于 Java 的配置
【发布时间】:2014-06-19 20:15:33
【问题描述】:

我是 SPring 3 MVC 的新手,正在尝试配置基于 java 的 Spring 3 MVC 应用程序。我正在为我的应用程序使用 servlet 2.5 容器。以下是我尝试过的。

以下是我基于 Maven 的项目结构:

web.xml

<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"> 

<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>  
  <welcome-file>index.jsp</welcome-file>  
</welcome-file-list>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
       <param-name>contextClass</param-name>
       <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>com.sprmvc.init.WebAppConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

</web-app>

WebAppConfig.java

@Configuration //Specifies the class as configuration  
@ComponentScan("com.sprmvc") //Specifies which package to scan  
@EnableWebMvc //Enables to use Spring's annotations in the code  
public class WebAppConfig {

@Bean
public UrlBasedViewResolver setupViewResolver() {  
    UrlBasedViewResolver resolver = new UrlBasedViewResolver();  
    resolver.setPrefix("/WEB-INF/pages/");  
    resolver.setSuffix(".jsp");  
    resolver.setViewClass(JstlView.class);  
    return resolver;
}

}

LinkController.java

@Controller
public class LinkController {

@RequestMapping(value="/hello")  
public ModelAndView goToHelloPage() {  
    ModelAndView view = new ModelAndView();  
    view.setViewName("hello"); //name of the jsp-file in the "page" folder  

    String str = "MVC Spring is here!";  
    view.addObject("message", str); //adding of str object as "message" parameter  

    return view;  
} 

}

index.jsp

<html>
<body>
<h1>Home page</h1>  
<p>This is a Home Page.</p>  
<p><a href="hello.html">Hello world link</a></p>  
</body>
</html>

hello.jsp

<html>
<body>
<p>Hello world: ${message}</p>  
<p>Well done!</p>
</body>
</html>

当我运行应用程序时,index.jsp 会正确显示。但是在点击index.jsp 页面中的link 时,我得到了一个404。

点击index.jsp页面的link触发的URL是http://localhost:8055/SpringMVCConfig/hello.html,由于资源不可用,因此出现404错误。

由于prefixsuffix 无法正常工作,我的基于java 的配置中有一些错误。请指教。

【问题讨论】:

  • URL 映射是*.htm 吗?你用hello.html 打电话给控制器,这是正确的吗?请检查 URL 映射

标签: java spring jsp spring-mvc web-applications


【解决方案1】:

你已经用值“/hello”映射了你的控制器,但是在 index.jsp 中你有

使用了 href="hello.html"。

所以它不会映射,你所有的映射配置都很好。

将 index.jsp 中的 href 值更改为 hello。

index.jsp

<html>
<body>
<h1>Home page</h1>  
<p>This is a Home Page.</p>  
<p><a href="hello">Hello world link</a></p>  
</body>
</html>

还可以更改 web.xml 中的 url 模式以映射任何请求,如下所示(/ 而不是 *.htm)

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

如果您想在示例中使用相同的 *.html 和 /hello.html,则需要在控制器中执行 redirect:url。

还要确保 *.htm 和 *.html 不同。

像下面这样使用

@RequestMapping("/hello.html")
public ModelAndView processForm(HttpServlet request, HttpServletResponse response){
    //process form data etc
    ModelAndView modelAndView = new ModelAndView("redirect:hello");                
    Map<Object, Object> model = modelAndView.getModel();
    modelAndView.addObject("error", "this.is.my.error.code");
    return modelAndView;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-11
    • 2014-04-14
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多