本案例的目标是加强对@Controller   @RequestMapping  @Resource  @Service 的感性认识,能过知道这几个注解是怎么用的,以及spring和springmvc的整合。

首先看一下本案例的总图:

上面的beans.xml的代码如下:它主要是对service层进行包扫描。

 <?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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"> <!-- 对所有的service进行包扫描 -->
<context:component-scan base-package="com.qls.service"/>
</beans>

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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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="com.qls.controller"/> <!-- 内部资源视图解析器 -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean> </beans>

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">
<!-- 以下是整合spring和springmvc 首先spring的配置,其次springmvc的配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- spring的配置 --> <!-- springmvc的配置: -->
<!-- 配置DispatcherServlet这个类 -->
<servlet>
<servlet-name>ouyangfeng</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 设置初始变量,指定类路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param> </servlet>
<servlet-mapping>
<servlet-name>ouyangfeng</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

ShowAllMemberController类代码如下:

 package com.qls.controller;

 import java.util.List;
import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import com.qls.domain.Student;
import com.qls.service.ShowAllMemberService; @Controller
/**
* @Controller这个注解声明这个类是一个控制器Controller.
* 但前提必须是在配置文件中进行包扫描。即:
* <context:component-scan base-package="com.qls.controller">
* 其中base-package中的com.qls.controller是ShowAllMemberController这个类所在的包。
*/
@RequestMapping("/ouyangfeng")
/**
* 对这个类ShowAllMemberController进行映射。
* 其实这句话相当于配置文件中的
* <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
" name="ouyangfeng">
* </bean>
* 如果不在类上面写上这句话:
* @RequestMapping("/ouyangfeng");则其默认以类名进行映射的.即@RequestMapping("/showAllMemberController");
*/
public class ShowAllMemberController {
@Resource
ShowAllMemberService showAllMemberService;
//这个RequestMapping()之所以写成.do的形式是因为在web.xml中配置DispatcherServlet这个类时写成.do的形式。
@RequestMapping("/listAll.do")
public String listAll(Map<String,Object> model){
List<Student> listAll = ShowAllMemberService.listAll();
model.put("sixi", listAll);
return "showAllMember";//这是个逻辑名。
}
}

ShowAllMemberService类的代码如下:

 package com.qls.service;

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.springframework.stereotype.Service; import com.qls.domain.Student; @Service
/**
* @Service这个注解说明这个类是Service层的,这个层是处理一些业务逻辑的。
* 前提条件必须也要在配置文件进行配置:包扫描
* 即:<context:component-scan base-package="com.qls.service">
* 对service进行包扫描一般是放在beans.xml中。不和springmvc-servlet.xml放在一块写。
*/
public class ShowAllMemberService {
//把map集合中放入10条数据:
private Integer id=0;
private static Map<Integer,Student> map=new HashMap<Integer, Student>();
//利用静态代码块把map中添加十条数据:
static{
for (int i = 0; i < 10; i++) {
Student student = new Student();
student.setId(i);
student.setName("tony"+i);
student.setId(20+i);
map.put(i, student);//这句话很重要,不要遗忘了。
}
}
//获取所有的记录:
public static List<Student> listAll(){
//把map集合放入ArrayList中。
return new ArrayList<Student>(map.values());
}
}

showAllMember.jsp的代码如下:

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
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 'showAllMember.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>&nbsp;
This is my show all member page. <br>
显示所有的人员:<br>
<table border="1">
<tr>
<td>序号</td>
<td>姓名</td>
<td>年龄</td>
</tr>
<c:forEach items="${sixi}" var="s">
<tr>
<td>${s.id+1}</td>
<td>${s.name}</td>
<td>${s.age}</td>
</tr>
</c:forEach> </table>
</body>
</html>

总结:1.通过上面的例子我们可以知道,访问一个web项目最重要的就是Controller。上面的Controller类通过注解@Resource调用了Service层的方法。

由此我们可以知道单独把Service层提取出来,是为了避免Controller(控制器层)显得过于多。不便以后维护。

2.@RequestMapping 翻译成中文就是请求映射,相当于配置文件中BeanNameUrlHandlerMapping.没有其他作用了。

@Controller.  @Service 只要对其进行包扫描spring 容器会自然把相应的类看成Controller 和Service,这个不需要程序员担心。

3.在整合Spring和springmvc是只需在web.xml文件中加上以下代码前提是你已经配置好了DispatchServlet这个类了。

 <!-- 以下是整合spring和springmvc 首先spring的配置,其次springmvc的配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

特别是这个ContextLoaderListener这个类不能遗忘了。

用Spring注解的方式实现对某个网页的访问的更多相关文章

  1. 使用Spring注解来简化ssh框架的代码编写

     目的:主要是通过使用Spring注解的方式来简化ssh框架的代码编写. 首先:我们浏览一下原始的applicationContext.xml文件中的部分配置. <bean id="m ...

  2. eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式

    eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式 右键项目打开Properties—>JA ...

  3. ehcache整合spring注解方式

    一.简介 在hibernate中就是用到了ehcache 充当缓存.spring对ehcache也提供了支持,使用也比较简单,只需在spring的配置文件中将ehcache的ehcache.xml文件 ...

  4. spring注解方式在一个普通的java类里面注入dao

    spring注解方式在一个普通的java类里面注入dao @Repositorypublic class BaseDaoImpl implements BaseDao {这是我的dao如果在servi ...

  5. Spring+AOP+Log4j 用注解的方式记录指定某个方法的日志

    一.spring aop execution表达式说明 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义 ...

  6. spring schedule定时任务(一):注解的方式

    我所知道的java定时任务的几种常用方式: 1.spring schedule注解的方式: 2.spring schedule配置文件的方式: 3.java类继承TimerTask: 第一种方式的实现 ...

  7. Spring学习笔记3——使用注解的方式完成注入对象中的效果

    第一步:修改applicationContext.xml 添加<context:annotation-config/>表示告诉Spring要用注解的方式进行配置 <?xml vers ...

  8. spring/java ---->记录和整理用过的注解以及spring装配bean方式

    spring注解 @Scope:该注解全限定名称是:org.springframework.context.annotation.Scope.@Scope指定Spring容器如何创建Bean的实例,S ...

  9. Spring 注解方式引入配置文件

    配置文件,我以两种为例,一种是引入Spring的XML文件,另外一种是.properties的键值对文件: 一.引入Spring XML的注解是@ImportResource @Retention(R ...

随机推荐

  1. java的八种基本数据类型

             据说表格的方式一目了然 一. java数据类型的取值范围如下: 注意:long型后如果不加 L 则默认为int型,float型如果不加 F 则默认为double型: 注意!注意!注意 ...

  2. php解决sql_server连接问题

    1.首先根据phpinfo()查看当前php版本以及线程安全情况(ts或者nts):2.下载sqlsrv扩展(适用32位php) 下载链接为https://www.microsoft.com/en-u ...

  3. vbox+Vagrant 入门指南

    Vagrant 简介 Vagrant 是一个用来构建和管理虚拟机环境的工具.Vagrant 有着易于使用的工作流,并且专注于自动化,降低了开发者搭建环境的时间,提高了生产力.解决了"在我的机 ...

  4. scrapy之分布式

    分布式爬虫 概念:多台机器上可以执行同一个爬虫程序,实现网站数据的分布爬取. 原生的scrapy是不可以实现分布式爬虫? a) 调度器无法共享 b) 管道无法共享 工具 scrapy-redis组件: ...

  5. LeetCode summary

    https://www.programcreek.com/2013/08/leetcode-problem-classification/ https://medium.com/algorithms- ...

  6. Android面试收集录17 Android进程优先级

    在安卓系统中:当系统内存不足时,Android系统将根据进程的优先级选择杀死一些不太重要的进程,优先级低的先杀死.进程优先级从高到低如下. 前台进程 处于正在与用户交互的activity 与前台act ...

  7. Android面试收集录9 IntentService详解

    一. 定义 IntentService是Android里面的一个封装类,继承自四大组件之一的Service. 二.作用 处理异步请求,实现多线程 三. 工作流程 注意:若启动IntentService ...

  8. 14,flask-sqlalchemy项目配置

    基于一个flask项目,加入flask-SQLAlchemy 1.加入falsk-sqlalchemy第三方组件 from flask import Flask # 导入Flask-SQLAlchem ...

  9. Java 基本数据类型总结一

    Java基本数据类型总结一 基本类型,或者叫做内置类型,是JAVA中不同于类的特殊类型.它们是我们编程中使用最频繁的类型.java是一种强类型语言,第一次申明变量必须说明数据类型,第一次变量赋值称为变 ...

  10. ADMX Migrator

    实用工具特别推荐ADMX MigratorLance Whitney 下载这篇文章的代码: ADMX Migrator (2765KB) 对于那些 使用组策略的人而言,他们自然非常熟悉如何使用管理模板 ...