-5

I have NetBeans Spring MVC project with Maven.But I got the following error: HTTP Status 404 - Not Found. How can I avoid this error? Hierarchy:

-FinalWebStore
   -Web Pages
     -WEB-INF
          +views
          web.xml
          mvc-dispatcher-servlet.xml
   -Source Packages
      +com.karans.finalwebstore.controllers
      +com.karans.finalwebstore.daoimps
      +com.karans.finalwebstore.daos
      +com.karans.finalwebstore.models
   +Dependecies
...

Here are my files: web.xml:

    <web-app id="WebApp_ID" 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 Web MVC Application</display-name>


    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>



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

</web-app>

mvc-dispatcher-servlet.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<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-4.0.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


   <bean name="/login"
        class="com.karans.finalwebstore.controllers.UserController" />


    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <bean id="ds"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/webstore" />
        <property name="username" value="root" />
        <property name="password" value="123456" />
    </bean>


    <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">  
        <property name="dataSource" ref="ds"></property>  
    </bean>  


    <bean id="ProductDAOimpl" class="com.karans.finalwebstore.daoimps.ProductDAOimp">

        <property name="template" ref="jt"></property>
    </bean>

    <bean id="UserDAOimpl"    class="com.karans.finalwebstore.daoimps.UserDAOimp">

        <property name="template" ref="jt"></property>  
    </bean>
    <bean id="CartDAOimpl" class="com.karans.finalwebstore.daoimps.CartDAOimp">

        <property name="template" ref="jt"></property>
    </bean>
    <bean id="UserController"    class="com.karans.finalwebstore.controllers.UserController">
        <property name="userdaoimp" ref="UserDAOimpl"></property>
    </bean>



</beans>

UserController.java:

    package com.karans.finalwebstore.controllers;
import com.karans.finalwebstore.daoimps.UserDAOimp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class UserController {
    @Autowired
    UserDAOimp userdaoimp;

    public void setUserdaoimp(UserDAOimp userdaoimp) {
        this.userdaoimp = userdaoimp;
    }

    public String login(){


        return "login";
    }


}

login.jsp:

<%-- 
    Document   : login
    Created on : Jul 15, 2017, 2:57:13 PM
    Author     : raziyeh
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>LOG IN PAGE</title>
    </head>
    <body>
        <h1>Hello World Taze avvaleshe!</h1>
    </body>
</html>

And I think other files are not important.

sunkuet02
  • 2,376
  • 1
  • 26
  • 33
ma98
  • 83
  • 3
  • 14
  • didn't my answer solve your issue ? I have seen that you have changed your code. If there is any other issue then, ask another question. It is not a way that, you change your question and ask in another question by editing your previous question – sunkuet02 Jul 25 '17 at 07:15
  • Sorry,I didn't know it is important for you.:( :( – ma98 Jul 25 '17 at 07:28
  • Yup It is important for community. If anyone in the StackOverflow community follow your question, they might be misguided. And for us(who have answered your question), we have answered according to your first edit. Hence, it have a bad impact on followers who may implement our given codes. If My answer makes sense to you, then accept so that, followers of your question may be benefited. Hope, you will understand the issue. – sunkuet02 Jul 25 '17 at 07:32
  • please read the question and answers https://meta.stackoverflow.com/q/309237/2315473 – sunkuet02 Jul 25 '17 at 19:57

2 Answers2

2

Change your login function as

@RequestMapping(value = "/")
public String login(){
    return "login";
}

Also change the web.xml file in the below portion :

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

You need to delete the below line from mvc-dispatcher-servlet.xml

<bean name="/login"
        class="com.karans.finalwebstore.controllers.UserController" />

Also, make sure that login.jsp file contains under the /WEB-INF/views/ folder.

sunkuet02
  • 2,376
  • 1
  • 26
  • 33
  • 1
    @masoomi, I have seen that you have edited your question after I answered your question and also un-accept my answer. It is improper and it impacts a bad effect the followers of that questions and answers. If you are falling into another issue then ask another question to the community. – sunkuet02 Jul 25 '17 at 07:25
0

Try the following:

Add the Spring MVC namespace to your mvc-dispatch-servlet.xml file xmlns:mvc="http://www.springframework.org/schema/mvc". You will need to update the xsi:schemaLocation as well with this "http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"

Second add <mvc:annotation-driven /> to the file. If I recall correctly, this enables processing of @RequestMapping annotations.

Third, remove the bean declared with the name "/login". It is the same type as the UserController declared at the bottom of the file.

Dave G
  • 9,639
  • 36
  • 41