簡體   English   中英

請求的資源不可用。 Tomcat 7 彈簧 mvc

[英]the requested resource is not available. tomcat 7 spring mvc

我正在跟進本教程https://www.javacodegeeks.com/2014/03/building-java-web-application-using-hibernate-with-spring.html我遇到了這個錯誤 >>>> 請求的資源可能找不到

web.xml 文件內容

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
    <servlet-name>studentHibernateServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/servletConfig.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>studentHibernateServlet</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/jpaContext.xml</param-value>
</context-param>

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

  <display-name>Archetype Created Web Application</display-name>
</web-app>

控制器內容

package com.github.elizabetht.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.github.elizabetht.model.Student;
import com.github.elizabetht.model.StudentLogin;
import com.github.elizabetht.service.StudentService;

@Controller
@SessionAttributes("student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value="/signup", method=RequestMethod.GET)
    public String signup(Model model) {
        Student student = new Student();        
        model.addAttribute("student", student);     
        return "signup";
    }

    @RequestMapping(value="/signup", method=RequestMethod.POST)
    public String signup(@Valid @ModelAttribute("student") Student student, BindingResult result, Model model) {        
        if(result.hasErrors()) {
            return "signup";
        } else if(studentService.findByUserName(student.getUserName())) {
            model.addAttribute("message", "User Name exists. Try another user name");
            return "signup";
        } else {
            studentService.save(student);
            model.addAttribute("message", "Saved student details");
            return "redirect:login.html";
        }
    }

    @RequestMapping(value="/login", method=RequestMethod.GET)
    public String login(Model model) {          
        StudentLogin studentLogin = new StudentLogin();     
        model.addAttribute("studentLogin", studentLogin);
        return "login";
    }

    @RequestMapping(value="/login", method=RequestMethod.POST)
    public String login(@Valid @ModelAttribute("studentLogin") StudentLogin studentLogin, BindingResult result) {
        if (result.hasErrors()) {
            return "login";
        } else {
            boolean found = studentService.findByLogin(studentLogin.getUserName(), studentLogin.getPassword());
            if (found) {                
                return "success";
            } else {                
                return "failure";
            }
        }

    }
}

請幫助我我是初學者

您的調度程序 servlet 只偵聽*.html 您可以調用具有此后綴的網址,即:

http://localhost::8080/[YourAppName]/[filename].html

或者您可以將 servlet 映射更改為更通用的內容:

<servlet-mapping>
 <servlet-name>studentHibernateServlet</servlet-name>
 <url-pattern>/*</url-pattern>
</servlet-mapping>

並調用即:

 http://localhost::8080/[YourAppName]/signup

更新

您的 servlet 版本設置為 2.5,這是非常舊的。 目前是3.1 例如,您可能希望將其更新為 Tomcat 7 的至少3.0 因此教程也有點過時了,搜索Spring 4 MVC以找到現代的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM