2015-06-05 3 views
1

Я использую Spring 3.2.7, у меня есть файл Login.jsp, который берет ввод и должен отображать вывод на hello.jsp, он принимает вход, но не показывает вместо этого он выдает ошибку как HTTP Status 404 - Not Found. Вот код:HTTP Status 404 - не найден весной 3.2.7

@Controller 
public class HelloWorldController { 

    @RequestMapping("/hello") 
    public ModelAndView helloWorld(HttpServletRequest request, HttpServletResponse response) { 
     String name = request.getParameter("name"); 
     String password = request.getParameter("password"); 
     if (password.equals("admin")) { 
      String message = "Hello " + name; 
      return new ModelAndView("hellopage", "message", message); 
     } else { 
      return new ModelAndView("errorpage", "message", "Sorry Username or Password Error"); 
     } 
    } 
} 

грузоотправитель-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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <context:component-scan base-package="mypack" /> 

    <bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
    p:prefix="/WEB-INF/jsp/" 
    p:suffix=".jsp" /> 

</beans> 

ответ

0

После наблюдения вашего Dispatcher-servlet.xml, я вижу, что вы не хватаете <mvc:annotation-driven/> Вашего обновленного кода будет выглядеть, как показано ниже

<?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-3.0.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd" 
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">            

     <mvc:annotation-driven/> 
     <context:component-scan base-package="mypack" /> 
     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" 
     p:suffix=".jsp" /> 
     </beans> 

Объяснение

<mvc:annotation-driven/> означает, что вы можете определить пружинные бобы зависимости фактически без необходимости указывать кучу элементов в XML или реализовать интерфейс или расширить базовую class.Means @Controller говорит весну, что класс содержит указанный методы, которые будут обрабатывать HTTP-запросы без необходимости реализации интерфейса контроллера или расширения подкласса, который реализует контроллер.

<context:component-scan base-package="mypack /> говорит весне, что должны искать путь класса для всех классов под mypack и взгляда на каждый класс, чтобы увидеть, если он имеет @Controller или @Repository или @Service или @Component и если это то весна будет регистрировать класс с бобовой фабрики, как если бы вы определили в в файлах конфигурации XML

+0

Спасибо так много, но теперь я получаю эту ошибку «в месте развертывания в C: \ Users \ Dibya \ Documents \ NetBeansProjects \ Spring2 \ build \ web GlassFish Server 4.1, deploy, null, false C: \ Users \ Dibya \ Documents \ NetBeansProjects \ Spring2 \ nbproject \ build-impl.xml: 1048: модуль не был развернут. Подробнее см. В журнале сервера. BUILD FAILED (общее время: 4 секунды) ". Я использую Netbeans 8.0 и jdk 8 – Dibya

+0

, вам нужно посмотреть журналы серверов .... – Dev

+0

проблема решена еще – Dev

0

Нет, я сделал изменения в Диспетчерский-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:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    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-3.1.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.1.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
"> 

<context:component-scan base-package="mypack" /> 
<mvc:annotation-driven /> 

<bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" 
     p:suffix=".jsp" /> 
</beans> 

still am getting an error as "Status 404 - Not Found" when i submit the form. 

Form: 

<form action="hello.htm" method="post"> 
     Name: <input type="text" name="name" /><br> 
     Password: <input type="password" name="password" /> 
     <input type="submit" value="Login" /> 
    </form> 

Вот web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/applicationContext.xml</param-value> 
</context-param> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>2</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>*.htm</url-pattern> 
</servlet-mapping> 
<session-config> 
    <session-timeout> 
     30 
    </session-timeout> 
</session-config> 
<welcome-file-list> 
    <welcome-file>Login.jsp</welcome-file> 
</welcome-file-list> 
</web-app>