本案例的目标是加强对@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. docker官方仓库下载镜像

    官方仓库镜像地址:https://hub.docker.com/search/ 以下载mysql为例 进入到详情页后我们看到有很多Tags 我们选择5.7.25版本进行下载 # docker pull ...

  2. idea中自定义设置xml的头文件的内容

    因为在idea中新建的xml默认的头文件,有时候并不是我们需要的这时候可以通过自定义来解决. 如搭建hibernate的实体类的映射xml. 首先 fiel→settings出现 如下框框 在上面搜索 ...

  3. Social Media Addiction【社交媒体上瘾】

    Social Media Addiction Children as young as ten are becoming dependent on social media for their sen ...

  4. C语言函数篇(四)函数的设计

    1. 函数设计的时候,如果使用到全局变量,就尽量通过参数的形式传递进来 也就是说,保持 函数 跟 外部的交互 只有 参数 和 返回值 2. 在有参数的情况下,或者有数值输入的时候,要先进行错误判断. ...

  5. POJ 2676 数独(DFS)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21612   Accepted: 10274   Specia ...

  6. spark的排序方法

    今天我们来介绍spark中排序的操作,spark的排序很简单,我们可以直接使用sortBy来进行,这个里面我们使用case clas,使用case class的好处是1.不用newjiukeyi 搞出 ...

  7. Android面试收集录1 Activity+Service

    1.Activity的生命周期 1.1.首先查看一下Activity生命周期经典图片. 在正常情况下,一个Activity从启动到结束会以如下顺序经历整个生命周期: onCreate()->on ...

  8. Kali Linux 搜狗输入法安装

    1.下载 搜狗输入法 for Linux http://pinyin.sogou.com/linux/ //有64位和32位的deb包 我这里下载的是 : sogoupinyin_2.1.0.0086 ...

  9. 12 KLT算法

    1 去除多余模块的 #-*- coding:utf-8 -*- ''' Lucas-Kanade tracker ==================== Lucas-Kanade sparse op ...

  10. 关于main与wmain函数

    最近写一个控制台程序,并且希望该控制台程序运行时不显示控制台窗口,于是在程序include语句下面加入如下代码 #pragma comment (linker,"/subsystem:\&q ...