资源在制作简单项目时未发现错误?

时间:2014-01-23 06:08:28

标签: java spring jsp spring-mvc

我正在使用MVC在Spring创建简单的hello world项目.Hi发现找不到此错误资源。我从此链接获取帮助。 http://crunchify.com/hello-world-example-spring-mvc-3-2-1/

可以复制调用文件。打开第一个index.jsp参与但是当我点击链接时它说资源找不到。

这是我的错误!enter image description here

我复制了所有代码。这是文件代码。 的的web.xml

  <?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>FirstWebProject</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

    <servlet>
        <servlet-name>crunchify</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
   <servlet-mapping>
    <servlet-name>crunchify</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>
    </web-app>

crunchify-servlet.xml中

  <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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.crunchify.controller" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

控制器

package com.crunchify.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CrunchifyHelloWorld {

    @RequestMapping("/welcome")
    public ModelAndView helloWorld() {

        String message = "<br><div align='center'>" + "<h1>Hello World, Spring 3.2.1 Example by Crunchify.com<h1> <br>";
        message += "<a href='http://crunchify.com/category/java-web-development-tutorial/'>More Examples</a>";
        return new ModelAndView("welcome", "message", message);
    }
}

的index.jsp

<html>
<head>
<title>Spring 3.2.1 MVC Series: Index - Crunchify.com</title>
</head>
<body>
    <br>
    <div align='center'>
    <h2>

            Your 1st Spring MCV Example <br> <br>
            <a href="welcome.jsp">Click here to See Welcome Message...</a>

        </h2>
            <br> by <a href="http://crunchify.com">Crunchify.com</a>
            </div>
</body>
</html>

的welcome.jsp

<html>
<head>
    <title>Spring 3.2.1 MVC Example: Hello World - Crunchify.com</title>
</head>
<body>
    ${message}
</body>
</html>

2 个答案:

答案 0 :(得分:0)

网址正在检查“welcome.html”而不是“welcome.jsp”

<servlet-mapping>
    <servlet-name>crunchify</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

<强>的index.jsp

<html>
<head>
<title>Spring 3.2.1 MVC Series: Index - Crunchify.com</title>
</head>
<body>
  <br>
  <div align='center'>
  <h2>

        Your 1st Spring MCV Example <br> <br>
        <a href="welcome.jsp">Click here to See Welcome Message...</a>

  </h2>
  <br> by <a href="http://crunchify.com">Crunchify.com</a>
  </div>
</body>
</html>

每当您使用创建Web应用程序时,请在web.xml文件中为欢迎文件列表创建一个条目

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

答案 1 :(得分:0)

仔细按照给定的教程进行以下更改

的web.xml

<url-pattern>*.html</url-pattern>

了解此here

的index.jsp

<a href="welcome.html">Click here to See Welcome Message...</a>

当您单击此链接时,将生成新的GET请求并将其重定向到映射控制器

控制器

@Controller
@RequestMapping("/welcome")
public class CrunchifyHelloWorld {

    @RequestMapping(method = RequestMethod.GET)
    public String helloWorld(ModelMap model, HttpServletRequest request) {
        String message = "<br><div align='center'>" + "<h1>Hello World, Spring 3.2.1 Example by Crunchify.com<h1> <br>";
        message += "<a href='http://crunchify.com/category/java-web-development-tutorial/'>More Examples</a>";
    //add message into module
    model.addAttribute("message", message);
        //name of jsp to show        
    return "welcome";
    }
}

然后调用你的helloWorld()方法,将你的消息添加到ModelMap并发送请求到welcome.jsp文件。

希望这会对你有帮助!