springmvc是一个基于spring的mvc框架,各种优点啥的用过就知道了.下面开始讲HelloWorldController的实现.

  1.开发环境搭建<导jar包+配置文件>

   1.1 新建web工程springmvc,导入springmvc所需的jar包,因为springmvc是基于spring的,所以必须包含spring的jar包,我用的版本是spring3.1.0.导入以下jar包:  

    

    1.2 配置web.xml   

      <!-- spring mvc配置 处理*.action和*.do请求 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载spring-servlet.xml配置文件,默认spring-servlet文件放在/WEB-INF目录下,为了便于管理,这里放到了src目录下 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <!-- spring 配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>

    1.3 spring-servlet.xml配置

      springmvc启动会去/WEB-INF/目录下加载web.xml文件${servlet-name}-servlet.xml的配置文件,在web.xml文件中我们配置的servlet-name为spring,并且配置了spring-servlet.xml的路径为src目录下.

      spring建议我们把springmvc的配置放到${servlet-name}-servlet.xml,把service层的配置放到xxx-service.xml,把dao层的配置放到xxx-database.xml配置文件中,也就是把配置分散开来.下面就是spring-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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- springmvc配置--> <!-- 自动扫描 -->
<context:component-scan base-package="cn.springmvc.controller"/> <!-- 视图解析类配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <!-- 如果返回的逻辑视图名为index,则视图解析器会找到webroot/jsps/index.jsp -->
          <property name="prefix">
<value>/jsps/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

    1.4 配置applicationContext.xml,将spring的其他配置放到applicationContext.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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- spring 其他配置 --> </beans>

  ok,基本配置都完了,开始写Controller.

  2.HelloWorldController

    controller是springmvc中负责处理请求的核心组件,等价于struts2中的Action.下面我们开始编写HelloWorldController.

     package cn.springmvc.controller;

        import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller("helloWorldController") //--->beanName
@RequestMapping(value="/hello") // controller的映射路径
public class HelloworldController { @RequestMapping(value="/say")// 方法的映射路径
public String sayHello(){
System.out.println("hello");
return "hello";
} }

    现在就可以发布项目,然后输入url访问了:localhost:8080/springmvc/hello/say.action

  现在感受到springmvc的好处了吧,哈哈!简洁的配置真让人爱不释手!

  补充一点,struts2中的Action是prototype的,所以Action没有线程安全问题.springmvc中的Controller默认是单例的,如果controller可能产生线程安全问题,可以在Controller上加上@Scope("prototype")注解,告诉spring,每次请求都创建新的controller对象.

springmvc入门之HelloWorld篇的更多相关文章

  1. SpringMVC入门一:helloWorld

    玩了一下SpringMVC, 感觉挺清爽的 好像没有struts那么臃肿( 可能是高级的东西我还不会用 哈 ) 例子中一共有俩方法: 一个Controller直接返回字串的方法, 另一个通过Dao层返 ...

  2. SpringMvc入门二----HelloWorld

    1. 导入需要的架包: 2. 配置web.xml,添加Servlet <servlet> <servlet-name>springmvc</servlet-name> ...

  3. SpringMVC.入门篇.一.HelloWorld

    SpringMVC.入门篇<一>HelloWorld 项目包结构如下: HelloController.java 代码 package com.charles.controller; im ...

  4. SpringMVC.入门篇《二》form表单

    SpringMVC.入门篇<二>form表单 项目工程结构: 在<springmvc入门篇一.HelloWorld>基础上继续添加代码,新增:FormController.ja ...

  5. SpringMvc快速入门之使用篇

    文章是为了结合工作需求来介绍springmvc,本文章只是切合实际的开发的场景对springmvc进行快速的入门介绍. 本篇文章不会对原理进行讲解.因为个人觉得有些对于新技术方面可以分为一下几个层次. ...

  6. Qt入门之基础篇 ( 二 ) :Qt项目建立、编译、运行和发布过程解析

    转载请注明出处:CN_Simo. 题解: 本篇内容主讲Qt应用从创建到发布的整个过程,旨在帮助读者能够快速走进Qt的世界. 本来计划是讲解Qt源码静态编译,如此的话读者可能并不能清楚地知道为何要静态编 ...

  7. Qt入门之基础篇(三):掌握Qt4的静态编译基本方法

    转载载请注明出处:CN_Simo. 导语: 前两章都提到过“静态编译”(Static Compilation),在Windows下一次静态编译差不多需要长达三个小时才能完成,而且还非常容易由于各种原因 ...

  8. Spring Boot 入门之基础篇(一)

    原文地址:Spring Boot 入门之基础篇(一) 博客地址:http://www.extlight.com 一.前言 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是 ...

  9. SpringMVC入门学习(二)

    SpringMVC入门学习(二) ssm框架 springMVC  在上一篇博客中,我简单介绍了一下SpringMVC的环境配置,和简单的使用,今天我们将进一步的学习下Springmvc的操作. mo ...

随机推荐

  1. [转] Hadoop管理员的十个最佳实践

    前言 接触Hadoop有两年的时间了,期间遇到很多的问题,既有经典的NameNode和JobTracker内存溢出故障,也有HDFS存储小文件问题,既有任务调度问题,也有MapReduce性能问题.遇 ...

  2. SQL Server数据库状态和文件状态

    数据库状态 (database states) 查询数据库的当前状态 : 1.查询所有数据库的状态 ,通过sys.databases目录视图的state_desc列 user master go se ...

  3. js new Date() 获取时间

    转载:https://www.cnblogs.com/xiaoshujiang/p/5518462.html 一,Date付给初始值,并构造new Date() Date 对象用于处理日期和时间.创建 ...

  4. Xcode 8 媒体权限

  5. 【原】使用Maven完成自动化打包并部署到Linux服务器下(Tomcat7)

    最近在使用maven,顺便尝试了下tomcat部署.网上找到了很多资料但是都不是最新的,所以贴上比较新的Tomcat7部署代码和配置,方便以后回顾-->测试OK. 1. 首先是配置Tomcat ...

  6. Sql Server 中使用日期遍历

    一个存储过程小案例,内容如下: declare @dt datetime set @dt='2016-01-01' while (@dt<='2016-12-31') begin -- 转换字符 ...

  7. day_02mysql表的约束设计

    首先我们复习第一天的主要内容: sql分类 知识概述 1) DDL(Data Definition Language)数据定义语言 用来定义数据库对象:数据库,表,列等.关键字:create, dro ...

  8. BZOJ3524: [Poi2014]Couriers(主席树)

    题意 题目链接 Sol 严格众数只会出现一次,那么建出主席树,维护子树siz,直接在树上二分即可 #include<bits/stdc++.h> #define LL long long ...

  9. CentOS7系列--1.4CentOS7服务

    CentOS7服务管理 1. 查看服务 1.1. 查看所有运行的服务 [root@centos7 ~]# systemctl -t service UNIT LOAD ACTIVE SUB DESCR ...

  10. 杀死进程-LeetCode-582

    英文版 582. Kill ProcessGiven n processes, each process has a unique PID (process id) and its PPID (par ...