首页 > 代码库 > springmvc 入门级教程

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="http://www.mamicode.com/styles.css">    -->  </head>    <body>    This is my JSP page. <br>  </body></html>

 

springmvc 入门级教程