Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring3MVC:基于Java的配置,使用servlet容器2.5.404_Java_Spring_Jsp_Spring Mvc_Web Applications - Fatal编程技术网

Spring3MVC:基于Java的配置,使用servlet容器2.5.404

Spring3MVC:基于Java的配置,使用servlet容器2.5.404,java,spring,jsp,spring-mvc,web-applications,Java,Spring,Jsp,Spring Mvc,Web Applications,我是Spring3MVC新手,正在尝试配置一个基于java的Spring3MVC应用程序。我正在为我的应用程序使用Servlet2.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

我是Spring3MVC新手,正在尝试配置一个基于java的Spring3MVC应用程序。我正在为我的应用程序使用Servlet2.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>
LinkController.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;
}

}
@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>
<html>
<body>
<p>Hello world: ${message}</p>  
<p>Well done!</p>
</body>
</html>

主页
这是一个主页。

hello.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>
<html>
<body>
<p>Hello world: ${message}</p>  
<p>Well done!</p>
</body>
</html>

你好,世界:${message}

干得好

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

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


由于
前缀
后缀
不起作用,我基于java的配置中出现了一些错误。请告知。

您已经为控制器添加了值“/hello”,但在index.jsp中您有

used href=“hello.html”

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

将index.jsp中的href值更改为just 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)


春天
/
如果您想在示例中使用相同的*.html和/hello.html,则需要在控制器中执行重定向: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;
}
@RequestMapping(“/hello.html”)
公共模型和视图处理表单(HttpServlet请求、HttpServletResponse响应){
//处理表单数据等
ModelAndView ModelAndView=newmodelandview(“重定向:hello”);
Map model=modelAndView.getModel();
addObject(“error”,“this.is.my.error.code”);
返回模型和视图;
}

是否URL映射为
*.htm
?您正在使用
hello.html
调用控制器,是否正确?请检查URL映射