错误404原始服务器未找到当前表示

时间:2019-04-10 21:53:19

标签: java spring model-view-controller eclipse-neon tomcat9

我正在尝试一个基本的spring mvc演示,当我在tomcat上运行它时,出现404错误,表明该servlet不存在。我的代码如下:

select IndexA.* from IndexA where IndexA.NAME like ('%stics%') and IndexA.ID in (1,2,3,4)

spring-mvc-demo-servlet

web.xml
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

家庭控制器

<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.luv2code.springdemo" />

<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>

<!-- Step 5: Define Spring MVC view resolver -->
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
</bean>

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试将您的示例与下面的可行解决方案匹配

web.xml,位于路径spring-tutorial-mvc \ WebContent \ WEB-INF

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>spring-tutorial-mvc</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file> 
    </welcome-file-list>

    <!-- - Location of the XML file that defines the root application context. 
        - Applied by ContextLoaderListener. -->

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/app-ctx.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--
        - Servlet that dispatches request to registered handlers (Controller implementations).
    -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

app-ctx.xml如下,路径:spring-tutorial-mvc \ WebContent \ WEB-INF

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   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"
   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/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
   <context:component-scan`enter code here` base-package="com.spring.tutorial" />
</beans>

mvc-config.xml,位于路径spring-tutorial-mvc \ WebContent \ WEB-INF

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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.xsd">

    <context:component-scan base-package="com.spring.tutorial"/>  
    <mvc:annotation-driven />
    <!-- 2. HandlerMapping : Used default handler mapping internally -->

    <!-- 3. ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/view/"/>
            <property name="suffix" value=".jsp"/>
    </bean>
</beans>

位于路径spring-tutorial-mvc \ src \ com \ spring \ tutorial \ controller的UserController.java

package com.spring.tutorial.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.giggal.spring.domain.User;
import com.giggal.spring.service.UserService;

@Controller
@RequestMapping(value = "/user/")
public class UserController {

    @Autowired
    UserService userService; // Service which will do all data
                                // retrieval/manipulation work

    // -------------------Retrieve All
    // Users--------------------------------------------------------

    @RequestMapping(value = "/getAllUsers/", method = RequestMethod.GET)
    public ModelAndView listAllUsers() {
        ModelAndView mav = new ModelAndView("user/user_list");
        List<User> users = userService.findAllUsers();
        mav.addObject("users", users);
        return mav;
    }

    // -------------------Retrieve Single
    // User--------------------------------------------------------

    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    public User getUser(@PathVariable("id") long id) {
        System.out.println("Fetching User with id " + id);
        User user = userService.findById(id);
        return user;
    }

    @RequestMapping(value = "/add/", method = RequestMethod.GET)
    public ModelAndView createUserForm() {
        System.out.println("Inside add...");
        ModelAndView mav = new ModelAndView("user/user_form");
        User user = new User();
        mav.addObject("command", user);
        mav.addObject("action", "Add Record");
        return mav;
    }

    // //-------------------Create a
    // User--------------------------------------------------------
    //
    @RequestMapping(value = "/create/", method = RequestMethod.POST)
    public ModelAndView createUser(@ModelAttribute("command") User user, HttpSession session) {
        System.out.println("Creating User " + user.getName());
        ModelAndView mav = new ModelAndView("user/master/user_form");
        try {
            if (userService.isUserExist(user)) {
                System.out.println("A User with name " + user.getName() + " already exist");
                // return new ResponseEntity<Void>(HttpStatus.CONFLICT);
            }

            if (user.getId() == null) {
                userService.saveUser(user);
                mav.setViewName("redirect:../add/?successMsg=Add successfully");
            } else {
                User currentUser = userService.findById(user.getId());
                currentUser.setName(user.getName());
                currentUser.setAge(user.getAge());
                currentUser.setSalary(user.getSalary());
                userService.updateUser(currentUser);
                mav.setViewName("redirect:../add/?successMsg=Update successfully");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            mav.addObject("command", user);
            mav.addObject("errorMsg", "Failed to add / update record");
        }
        return mav;

    }

    // ------------------- Delete a User
    // --------------------------------------------------------

    @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
    public void deleteUser(@PathVariable("id") long id) {
        System.out.println("Fetching & Deleting User with id " + id);
        User user = userService.findById(id);

        userService.deleteUserById(id);
    }

    // ------------------- Delete All User
    // --------------------------------------------------------

    @RequestMapping(value = "/user/", method = RequestMethod.DELETE)
    public void deleteAllUsers() {
        System.out.println("Deleting All Users");
        userService.deleteAllUsers();

    }

}

此处的网址为http://localhost:// user / getAllUsers port:服务器的端口 context-root是Web应用程序的上下文路径