1
votes

I wanna know how to servlet call html and jsp file page. I'm using spring mvc with servlet

when I run

localhost:8080/ -> it's running correctly

localhost:8080/htmlPage -> it's not working return error 404 not found

Here my path

enter image description here

Here my code

servlet mvc-dispatcher-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";
    }

}
2
check whether the request is reaching your controller.Anand
How do you deploy your application ?RicoZ

2 Answers

0
votes
    <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";
    }
}

please change your url pattern with ".so". all ".so" request are consider as spring request and handle by spring dispatcher servlet.

0
votes

Your call is wrong. First there should be an app name and then request name.
You have to call like below :

localhost:8080/YourAppName/htmlPage