1、web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param> --Spring Controller 配置
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet> --Spring mvc 默认调度
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>sh</servlet-name>
<url-pattern>/servlet/sh</url-pattern>
</servlet-mapping>
</web-app>

load-on-startup表示启动容器时初始化该Servlet;

url-pattern表示哪些请求交给Spring Web MVC处理,配置Spring的配置文件,默认DispatcherServlet会加载WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件.

2、dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.javahash.spring.controller"></context:component-scan> --配置扫描包
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value=""></property> --路径前缀
<property name="suffix" value=".jsp"></property> --路径后缀
</bean>
</beans>

3、Controller 代码

package com.javahash.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import java.util.logging.Logger; /**
* Created by Administrator on 2014/11/8.
*/
@Controller
public class HelloWorldController { @RequestMapping("/hello")
public String hello(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "helloworld";
}
}

最新文章

  1. 我为NET狂官方面试题
  2. STL_lower_bound&amp;upper_bound用法
  3. Deep Learning 22:总结一些deep learning的基本知识
  4. Posterior visual bounds retrieval for the Plato framework
  5. 【Hibernate】Hibernate系列2之Session详解
  6. .getBoundingClientRect()
  7. OPENCV第一篇
  8. HDU 5772 String problem
  9. 【python】字符串
  10. 9.3.3 scrapy 框架
  11. 如何使用FluentMigrator进行数据库迁移
  12. WinForm界面设计优化过程
  13. JAVA 平时作业一
  14. vsftp搭建文档
  15. 秦殇 xbm buffer
  16. Android多线程编程&lt;一&gt;Android中启动子线程的方法
  17. transition: 0.2s all ease;
  18. RSA公钥文件解密密文的原理分析
  19. Zookeeper的实际应用
  20. linux网络操作 配置文件

热门文章

  1. vue 百度地图实现标记多个maker,并点击任意一个maker弹出对应的提示框信息, (附: 通过多个地址,标记多个marker 的 方法思路)
  2. POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)
  3. Pyinstaller安装以及参数使用
  4. unity中尽量规避的C#写法
  5. Iowait的成因、对系统影响及对策--systemtap
  6. centos7 设置tomcat自启动
  7. jconsole工具使用
  8. Redis集群简记
  9. iOS开发-获取设备型号信息
  10. CDH:cdh5环境搭建