Neeraj Pandey
Neeraj Pandey

Reputation: 197

HTTP Status 404 - Spring MVC

I just created a sample spring mvc project in spring tool suit (without any modification) and when i am executing it i am getting the output as per index.jsp. But after that i just add one index.html page in view folder and i made following two changes

  1. In controller class i added : return 'index';

  2.  <beans:property name="prefix" value="/WEB-INF/views/" />
     <beans:property name="suffix" value=".html" />
    

while executing the code after change on Pivotal Server Developer Edition v3.1 i am getting HTTP Status 404 (The requested resource is not available error.)

And as per my knowledge there is no other change required. Please provide some suggestion. Thanks in advance

HomeController.java

@Controller

public class HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

/**
 * Simply selects the home view to render by returning its name.
 */
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate );

    return "index";
}

}

servlet-context.xml

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".html" />
</beans:bean>

<context:component-scan base-package="com.srio.tata" /> 

web.xml

contextConfigLocation /WEB-INF/spring/root-context.xml

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

roo-context.xml

Upvotes: 0

Views: 353

Answers (3)

Nadym Baba
Nadym Baba

Reputation: 51

IN web.xml change url pattern from

<url-pattern>/</url-pattern>

to

<url-pattern>/*.html</url-pattern>

Upvotes: 0

Abhash Upadhyaya
Abhash Upadhyaya

Reputation: 727

Since .html files are static and do not require processing by a servlet then it is more efficient, and simpler, to use an <mvc:resources/> mapping. This requires Spring 3.0.4+.

For example:

<mvc:resources mapping="/static/**" location="/static/" />

which would pass through all requests starting with /static/ to the webapp/static/ directory.

So by putting index.html in webapp/static/ and using return "static/index.html"; from your method, Spring should find the view.

Refer this question - https://stackoverflow.com/a/15480301/4531387

Upvotes: 0

Tran Vinh Quang
Tran Vinh Quang

Reputation: 595

Did you config viewResolver

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

Upvotes: 1

Related Questions