如何运行简单的hello world spring应用程序?

时间:2015-09-17 15:44:48

标签: spring-mvc

我有这个简单的hello world spring appplication通过spring工具编码,当我想访问jsp时,我得到错误404。我一步一步地按照主要教程进行操作,但仍未找到解决此问题的任何方法。有什么建议吗?

**Hello Controller**

package com.pluralsight.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller 
public class HelloController {

@RequestMapping(value="/greeting")   //url navigation
public String sayHello(Model model){  // model = mapmash for servlet
    model.addAttribute("greeting","Hello World");
    return "hello";
}
}

**hello.jsp**

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>${greeting}</h1>
</body>
</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>myFristApplication</servlet-name>
<servlet-class>org.springframework.web.servlet.DisplatcherServlet</servlet- class>
<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>

<servlet-mapping>
 <servlet-name>myFristApplication</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
   <display-name>Archetype Created Web Application</display-name>
</web-app>


   **servlet-config.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.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-3.2.xsd">


<mvc:annotation-driven /><!-- annotation declaration -->
<context:component-scan base-package="com.pluralsight.controller" /> <!-- Where is the annotation ? A: base-package -->

<bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />
       <mvc:resources mapping="/html/**" location="/WEB-INF/jsp/" />


</beans>

0 个答案:

没有答案
相关问题