我制作了春季网络应用示例项目。我做了所有基本配置,使用基于java的配置和servlet 3.0 web.xml。 但是当我点击网址后所有这些配置它给我404错误。 可能是什么问题呢。有人可以帮帮我吗?
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee;
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>SpringPracs</display-name>
<!-- <welcome-file-list> -->
<!-- <welcome-file>/jspfiles/index.jsp</welcome-file> -->
<!-- </welcome-file-list> -->
<servlet>
<servlet-name>home</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>home.practise.configuration.SpringConfigs</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/practise/*</url-pattern>
</servlet-mapping>
</servlet>
</web-app>
配置java类:
package home.practise.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@ComponentScan(basePackages = "home.practise.application")
@EnableWebMvc
@PropertySource("classpath:practise.properties")
public class SpringConfigs {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setPrefix("/jspfiles/");
internalResourceViewResolver.setSuffix(".jsp");
return internalResourceViewResolver;
}
}
控制器:
package home.practise.application;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("homeController")
public class HomeController {
@RequestMapping("homePage.ht")
public ModelAndView takeMeToHomePage(){
ModelAndView modelAndView = new ModelAndView("index");
return modelAndView;
}
}
当我尝试访问此网址时
http://localhost:8080/SpringPracs/practise/homeController/homePage.ht
它给了我404错误。虽然当我直接访问jsp页面时,它会显示该页面。
答案 0 :(得分:1)
首先,当Spring参考手册中的所有示例使用以/
开头的绝对 URL时,您使用相对URL。我认为它可以工作,但请帮自己一个忙,并遵循Spring的例子......
但实际问题是您将Spring DispatcherServlet
映射到/practise/*
。这是完全正确的,但是你必须声明你的控制器回应/practise/...
:
@Controller
@RequestMapping("/practise/homeController")
public class HomeController {
@RequestMapping("/homePage.ht")
public ModelAndView takeMeToHomePage(){
...
}
}
编辑:
作为替代方案,您可以将控制器映射到/homeController
并将调度程序servlet映射到/