本案例的目标是加强对@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. 【jQuery】阶段(插入、复制、替换、删除)

    <p>你好!</p> 你最喜欢的水果是? <ul> <li title="苹果">苹果</li> <li titl ...

  2. 012---Django的用户认证组件

    知识预览 用户认证 回到顶部 用户认证 auth模块 ? 1 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的 ...

  3. 7-1 寻找大富翁 PTA 堆排序

    7-1 寻找大富翁 (25 分) 胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假设给出N个人的个人资产值,请快速找出资产排前M位的大富翁. 输入格式: 输入首 ...

  4. [Bzoj4818]序列计数(矩阵乘法+DP)

    Description 题目链接 Solution 容斥原理,答案为忽略质数限制的方案数减去不含质数的方案数 然后矩阵乘法优化一下DP即可 Code #include <cstdio> # ...

  5. C语言关键词解释

    51单片机关键词 code code的作用是告诉单片机,我定义的数据要放在ROM(程序存储区)里面,写入后就不能再更改

  6. Java语言基础---两变量间的交换

    使用中间变量交换两个变量的值 int a = 10 , b = 11 , m; m = a; a = b; b = m; 不使用中间变量交换两个变量的值 int a = 10; int b = 11; ...

  7. com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 's.areaname' in 'field list'错误

    在使用mybatis框架做查询的时候,出现了如下错误: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown colum ...

  8. CC3200模块的内存地址划分和bootloader,启动流程(二)

    1. 首先启动内部ROM固化的BOOT,然后这个ROM启动需要使用内存空间0X2000 0000 --- 0X2000 4000共16K的空间.一级BOOT的作用是串口升级和驱动库. 2. 然后是二级 ...

  9. 《数据结构与算法分析:C语言描述》复习——第八章“并查集”——并查集

    2014.06.18 14:16 简介: “并查集”,英文名为“union-find set”,从名字就能看出来它支持合并与查找功能.另外还有一个名字叫“disjoint set”,中文名叫不相交集合 ...

  10. selenium 使用谷歌浏览器模拟wap测试

    /** * 使用谷歌浏览器模拟手机浏览器 * @param devicesName * @author xxx * 创建时间:2017-06-15,更新时间:2017-06-15 * 备注 */ pu ...