Spring:源服务器未找到目标资源的当前表示,或不愿透露其存在。

huangapple 未分类评论56阅读模式
英文:

Spring: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists

问题

StudentController.java

package com.learningspring;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.servlet.ModelAndView;

@Controller
public class StudentController {
    @RequestMapping(value = "/student", method = RequestMethod.GET)
    public ModelAndView student(){
        return new ModelAndView("student", "command", new Student());
    }

    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
    public String addStudent( @ModelAttribute("SpringWeb") Student student, ModelMap model){
        model.addAttribute("name", student.getName());
        model.addAttribute("age", student.getAge());
        model.addAttribute("id", student.getId());

        return "result";
    }
}

web.xml

<web-app id = "WebApp_ID" version = "2.4"
         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_2_4.xsd">
    
    <display-name>Spring MVC Form Handling</display-name>
    
    <servlet>
        <servlet-name>StudentSpring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>StudentSpring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>

StudentSpring-servlet.xml

<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"
       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">
    
    <context:component-scan base-package = "com.learningspring" />
    
    <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name = "prefix" value = "/WEB-INF/jsp/" />
        <property name = "suffix" value = ".jsp" />
    </bean>
    
</beans>

student.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>Spring MVC Form</title>
</head>
<body>
    <h2>Student Information</h2>
    <form:form method="post" action="/HelloWeb/addStudent">
        <table>
            <tr>
                <td><form:label path="name">Name</form:label></td>
                <td><form:input path="name" /></td>
            </tr>
            <tr>
                <td><form:label path="age">Age</form:label></td>
                <td><form:input path="age" /></td>
            </tr>
            <tr>
                <td><form:label path="id">ID</form:label></td>
                <td><form:input path="id" /></td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="Submit" />
                </td>
            </tr>
        </table>
    </form:form>
</body>
</html>

result.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<html>
<head>
    <title>Spring MVC Form</title>
</head>
<body>
    <h2>Submitted Student Information</h2>
    <table>
        <tr>
            <td>Name</td>
            <td>${name}</td>
        </tr>
        <tr>
            <td>Age</td>
            <td>${age}</td>
        </tr>
        <tr>
            <td>ID</td>
            <td>${id}</td>
        </tr>
    </table>
</body>
</html>
英文:

I tried following a tutorial for Spring but I can't get my app working on Tomcat. Every link I try is a 404 with the following error:

> The origin server did not find a current representation for the target
> resource or is not willing to disclose that one exists.

I tried every possible combination of url. Here's my code. Even by copy-pasting the tutorial it doesn't work.

StudentController.java

package com.learningspring;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.servlet.ModelAndView;

@Controller
public class StudentController {
    @RequestMapping(value = &quot;/student&quot;, method = RequestMethod.GET)
    public ModelAndView student(){
        return new ModelAndView(&quot;student&quot;, &quot;command&quot;, new Student());
    }

    @RequestMapping(value = &quot;/addStudent&quot;, method = RequestMethod.POST)
    public String addStudent( @ModelAttribute(&quot;SpringWeb&quot;) Student student, ModelMap model){
        model.addAttribute(&quot;name&quot;, student.getName());
        model.addAttribute(&quot;age&quot;, student.getAge());
        model.addAttribute(&quot;id&quot;, student.getId());

        return &quot;result&quot;;
    }
}

web.xml

&lt;web-app id = &quot;WebApp_ID&quot; version = &quot;2.4&quot;
         xmlns = &quot;http://java.sun.com/xml/ns/j2ee&quot;
         xmlns:xsi = &quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation = &quot;http://java.sun.com/xml/ns/j2ee
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    &lt;display-name&gt;Spring MVC Form Handling&lt;/display-name&gt;

    &lt;servlet&gt;
        &lt;servlet-name&gt;StudentSpring&lt;/servlet-name&gt;
        &lt;servlet-class&gt;
            org.springframework.web.servlet.DispatcherServlet
        &lt;/servlet-class&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;

    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;StudentSpring&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;

&lt;/web-app&gt;

StudentSpring-servlet.xml

    &lt;beans xmlns = &quot;http://www.springframework.org/schema/beans&quot;
           xmlns:context = &quot;http://www.springframework.org/schema/context&quot;
           xmlns:xsi = &quot;http://www.w3.org/2001/XMLSchema-instance&quot;
           xsi:schemaLocation = &quot;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">
    
        &lt;context:component-scan base-package = &quot;com.learningspring&quot; /&gt;
    
        &lt;bean class = &quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;
            &lt;property name = &quot;prefix&quot; value = &quot;/WEB-INF/jsp/&quot; /&gt;
            &lt;property name = &quot;suffix&quot; value = &quot;.jsp&quot; /&gt;
        &lt;/bean&gt;
    
    &lt;/beans&gt;

student.jsp

&lt;%@ taglib prefix=&quot;form&quot; uri=&quot;http://www.springframework.org/tags/form&quot; %&gt;
&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;

&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Spring MVC Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h2&gt;Student Information&lt;/h2&gt;
    &lt;form:form method=&quot;post&quot; action=&quot;/HelloWeb/addStudent&quot;&gt;
        &lt;table&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;form:label path=&quot;name&quot;&gt;Name&lt;/form:label&gt;&lt;/td&gt;
                &lt;td&gt;&lt;form:input path=&quot;name&quot; /&gt;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;form:label path=&quot;age&quot;&gt;Age&lt;/form:label&gt;&lt;/td&gt;
                &lt;td&gt;&lt;form:input path=&quot;age&quot; /&gt;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;form:label path=&quot;id&quot;&gt;ID&lt;/form:label&gt;&lt;/td&gt;
                &lt;td&gt;&lt;form:input path=&quot;id&quot; /&gt;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td colspan=&quot;2&quot;&gt;
                    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;
    &lt;/form:form&gt;
&lt;/body&gt;
&lt;/html&gt;

result.jsp

&lt;%@ page contentType=&quot;text/html;charset=UTF-8&quot; language=&quot;java&quot; %&gt;
&lt;%@page isELIgnored = &quot;false&quot; %&gt;
&lt;%@taglib uri = &quot;http://www.springframework.org/tags/form&quot; prefix = &quot;form&quot;%&gt;

&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Spring MVC Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h2&gt;Submitted Student Information&lt;/h2&gt;
    &lt;table&gt;
        &lt;tr&gt;
            &lt;td&gt;Name&lt;/td&gt;
            &lt;td&gt;${name}&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Age&lt;/td&gt;
            &lt;td&gt;${age}&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;ID&lt;/td&gt;
            &lt;td&gt;${id}&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2020年7月27日 22:31:10
  • 转载请务必保留本文链接:https://java.coder-hub.com/63117530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定