springmvc 入门级教程

1、先看下目录结构

2、引用所需的jar文件

 

3、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>springmvc</servlet-name>              
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      
 </servlet>             
 <servlet-mapping>              
 <servlet-name>springmvc</servlet-name>             
  <!-- 监听所有请求 -->              
  <url-pattern>/</url-pattern>     
 </servlet-mapping>
</web-app>

4、springmvc-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:context="http://www.springframework.org/schema/context"       
xmlns:mvc="http://www.springframework.org/schema/mvc"       
xsi:schemaLocation="http://www.springframework.org/schema/mvc         
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd              
     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">       
     <!-- 扫描所有的 controller -->       
     <context:component-scan base-package="controller" />       
     <!-- 启动注解驱动 SpringMVC 功能 -->       
     <mvc:annotation-driven />       
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
     <property name="prefix" value="/views/" />             
      <property name="suffix" value=".jsp" />
      </bean>
 </beans>

5、根目录下新寻views文件夹在里面新建home子文件夹然后再新建 index.jsp(普通页面)
index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    This is my JSP page. <br>
  </body>
</html>

 HomeController内容如下:

package controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class HomeController {

    
    /*
     * 首页
     */
    @RequestMapping(value="/index")
    public String index(Model model){        
        return "/index";
    }
}

启动开发工具或tomcat之后访问:http://localhost:8080/index 就能浏览到index.jsp里面的内容。

posted on 2014-12-04 13:14  lvlv岁月流逝  阅读(259)  评论(2编辑  收藏  举报

导航