Spring MVC没有找到WebApplicationContext:没有注册ContextLoaderListener

时间:2016-01-20 09:52:46

标签: java spring jsp spring-mvc

我正在尝试在Spring MVC中创建一个项目。我在这里得到错误。

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
    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_3_0.xsd">


    <servlet>
        <servlet-name>welcome</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>welcome</servlet-name>
        <url-pattern>/</url-pattern>
     </servlet-mapping>
     <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
     </welcome-file-list>
</web-app>

Controller.java

package java4s;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.ArrayList;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;

import java4s.EmployeeService;
@Controller
public class HelloController {  

    @Autowired
    EmployeeService emp_service;

    @RequestMapping("/welcome")
        public ModelAndView helloWorld(@ModelAttribute("userForm") Employee employee, ModelMap model) {

            String message =  "Welcome to Java4s.com Spring MVC 4.1.1 Sessions";
                   message += "<br>You Did it....!";
                   List<String> professionList = new ArrayList();
                    professionList.add("Developer");
                    professionList.add("Designer");
                    professionList.add("IT Manager");
                    model.put("professionList", professionList);
            return new ModelAndView("welcomePage", "welcomeMessage", new Employee());
        }
        @RequestMapping(value = "/addemployee", method=RequestMethod.POST)
        @DateTimeFormat(pattern="MM/dd/yyyy")
    public ModelAndView submitForm(ModelMap model, @ModelAttribute("userForm") Employee employee/*, BindingResult errors*/) {

            model.addAttribute("username", employee.getUsername());
            model.addAttribute("password", employee.getPassword());
            model.addAttribute("birthdate", employee.getBirthdate());
            model.addAttribute("email", employee.getEmail());
            model.addAttribute("professionList", employee.getProfession());
            model.addAttribute("userForm", new Employee());

            emp_service.saveData(employee);
            return new ModelAndView("RegisterSuccess",model);
    }

}

我收到错误

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?

当我添加

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

它给出错误

HTTP Status 404 - /SampleSpringHello/index.jsp

web.xml有问题吗?

0 个答案:

没有答案
相关问题