2015-07-28 65 views
1

我想知道如何在servlet中调用html和jsp文件页面。 我使用Spring MVC的使用Servletjsp和html之间的spring servlet

当我运行

本地主机:8080/- >它的正常运行

本地主机:8080/htmlPage - >它不工作返回错误404未找到

这里我的路径

enter image description here

这里我的代码

的servlet MVC-调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

<context:component-scan base-package="com.springapp.mvc"/> 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/pages/"/> 
    <property name="suffix" value=""/> 
</bean> 

<mvc:resources mapping="/resources/**" location="/resources/"/> 
<mvc:annotation-driven /> 

的web.xml

<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

<display-name>Spring MVC Application</display-name> 

<servlet> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

HelloController.java

@Controller 
public class HelloController { 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String wellcome(ModelMap model) { 
     model.addAttribute("message", "Hello world!"); 
     return "hello.jsp"; 
    } 

    @RequestMapping(value="/htmlPage", method = RequestMethod.GET) 
    public String startHtml(){ 
     return "hello.html"; 
    } 

} 
+0

检查请求是否到达您的控制器。 – Anand

+0

你如何部署你的应用程序? –

回答

0

您的电话有误。首先应该有一个应用程序名称,然后请求名称。
有一个像下面致电:

localhost:8080/YourAppName/htmlPage 
+0

它不工作仍然错误404 –

+0

你尝试过'localhost:8080/YourAppName /'? – vivekpansara

+0

是的,但不工作,我不知道为什么? –

0
<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

<display-name>Spring MVC Application</display-name> 

<servlet> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <url-pattern>*.so</url-pattern> 
</servlet-mapping> 

@Controller 
public class HelloController { 

    @RequestMapping(value = "/home.so", method = RequestMethod.GET) 
    public String wellcome(ModelMap model) { 
     model.addAttribute("message", "Hello world!"); 
     return "hello.jsp"; 
    } 

    @RequestMapping(value="/htmlPage.so", method = RequestMethod.GET) 
    public String startHtml(){ 
     return "hello.html"; 
    } 
} 

请您用 “。那么” 你的URL模式。所有“ .so”请求都被视为spring请求和由spring调度器servlet处理。

+0

现在用localhost命中浏览器:8080/YourAppName/home.so – vikash

+0

即使您没有在请求中添加“.so”,它也会处理请求。 – vivekpansara

+0

是的,它是正确的。但它是标准的编程方式。当您配置另一个框架时,您可以通过请求扩展轻松捕获所有内容。 – vikash