Spring MVC http 500 error apache

2019-03-03 16:03发布

问题:

Above is the directory hierarchy of my program

I am new to spring and learning MVC concepts I have written a program which takes input(Name) into a text box and prints Hello...'name'. Tha following is my directory structure and the various files I have created.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>MVC_HelloWorld</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list>

  <!-- default configuration -->
  <servlet>
   <servlet-name>HelloWorld</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>*.ap</url-pattern> <!-- this same extension should bbe used in form action -->
  </servlet-mapping>
</web-app>

HelloWorld-servlet.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
<!-- default handler mapping -->
<!--  file should be created under web inf annd it's view resolver file -->

  <!-- handler(Not rqd in case of default handler) -->
  <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

  <!-- controller configuration -->
<bean name="/HelloWorld.ap" class="controller.HelloController"> <!-- mapping url pattern to controller class using 'name' -->

<!-- view resolver -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" vlaue="/"/> <!-- default location (prefix used foor rqd page locations) -->
       |<property name="sufix" value=".jsp"/> <!-- sufix used forr rqd page extensions -->

</bean>

</bean>

</beans>

HelloController.java

package controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.sun.javafx.collections.MappingChange.Map;

public class HelloController implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception {

        String name=req.getParameter("name");

        Map m= new HashMap();   // creating output object
           m.put("msg","Hello..."+name);

          ModelAndView mav=new ModelAndView("success"+m);



        return mav;
    }

}

index.jsp

<h1> Hello World</h1>

<form action="./hello.ap">

NAME: <input type="text" name="name">
      <input type="Submit" value="Say Hello">
</form>

success.jsp

${msg}

when I am running this code the index.jsp page is running properly bur upon further execution It shows Error 404. what's wrong with the code..?? I am using Eclipse oxygen in that apache 8.5

回答1:

Your servlet name in definition is HelloWorld but in mapping servlet, is hello. These names must be the same.



回答2:

<servlet>
   <servlet-name>HelloWorld</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>*.ap</url-pattern> <!-- this same extension should bbe used in form action -->
  </servlet-mapping>
</web-app>

here you have used HelloWorld as the servlet name previously and you referring to that as hello later on which is not correct so please correct that just change the hello in servelt-mapping to HelloWorld and access the servlet as HelloWorld.ap it will work.