用Spring注解的方式实现对某个网页的访问
本案例的目标是加强对@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>
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注解的方式实现对某个网页的访问的更多相关文章
- 使用Spring注解来简化ssh框架的代码编写
目的:主要是通过使用Spring注解的方式来简化ssh框架的代码编写. 首先:我们浏览一下原始的applicationContext.xml文件中的部分配置. <bean id="m ...
- eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式
eclipes的Spring注解SequenceGenerator(name="sequenceGenerator")报错的解决方式 右键项目打开Properties—>JA ...
- ehcache整合spring注解方式
一.简介 在hibernate中就是用到了ehcache 充当缓存.spring对ehcache也提供了支持,使用也比较简单,只需在spring的配置文件中将ehcache的ehcache.xml文件 ...
- spring注解方式在一个普通的java类里面注入dao
spring注解方式在一个普通的java类里面注入dao @Repositorypublic class BaseDaoImpl implements BaseDao {这是我的dao如果在servi ...
- Spring+AOP+Log4j 用注解的方式记录指定某个方法的日志
一.spring aop execution表达式说明 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义 ...
- spring schedule定时任务(一):注解的方式
我所知道的java定时任务的几种常用方式: 1.spring schedule注解的方式: 2.spring schedule配置文件的方式: 3.java类继承TimerTask: 第一种方式的实现 ...
- Spring学习笔记3——使用注解的方式完成注入对象中的效果
第一步:修改applicationContext.xml 添加<context:annotation-config/>表示告诉Spring要用注解的方式进行配置 <?xml vers ...
- spring/java ---->记录和整理用过的注解以及spring装配bean方式
spring注解 @Scope:该注解全限定名称是:org.springframework.context.annotation.Scope.@Scope指定Spring容器如何创建Bean的实例,S ...
- Spring 注解方式引入配置文件
配置文件,我以两种为例,一种是引入Spring的XML文件,另外一种是.properties的键值对文件: 一.引入Spring XML的注解是@ImportResource @Retention(R ...
随机推荐
- PXE+DHCP+TFTP+Cobbler 无人值守安装centos 7
Cobbler(补鞋匠)是通过将DHCP.TFTP.DNS.HTTP等服务进行集成,创建一个中央管理节点,其可以实现的功能有配置服务,创建存储库,解压缩操作系统媒介,代理或集成一个配置管理系统,控制电 ...
- PHP实现的一个时间帮助类
背景 工作的过程中经常会遇到各种时间类的操作,因此封装了一个帮助工具类,提高代码的复用率 主要功能 根据相差的天数获取连续的时间段 /** * 根据相差的天数获取所有连续的时间段 * @param $ ...
- POJ 2084 Catalan
Game of Connections Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8772 Accepted: 43 ...
- python基础之函数参数、嵌套、返回值、对象、命名空间和作用域
函数的使用原则 函数的使用必须遵循:先定义后使用的原则 函数的定义,与变量的定义是相似的,如果没有事先定义函数而直接引用就相当于在引用一个不存在变量名 定义阶段:只检测语法,不执行代码,当出现语法错误 ...
- ORA-01122: 数据库文件 1 验证失败
1.SQL>shutdown abort 如果数据库是打开状态,强行关闭 2.SQL>sqlplus / as sysdba 3.SQL>startupORACLE 例程已经启动. ...
- android apk瘦身之 图片压缩 tinypng
参考地址: http://blog.csdn.net/jy692405180/article/details/52409369 http://www.tuicool.com/articles/BraI ...
- 剑指Offer - 九度1508 - 把字符串转换成整数
剑指Offer - 九度1508 - 把字符串转换成整数2014-02-06 23:46 题目描述: 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 输入: 输入可能包含多个测试样例 ...
- USACO Section1.3 Prime Cryptarithm 解题报告
crypt1解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- Kotlin的数据类:节省很多行代码(KAD 10)
作者:Antonio Leiva 时间:Jan 25, 2017 原文链接:https://antonioleiva.com/data-classes-kotlin/ 在前面的文章中,我们已经见到了类 ...
- net对象的克隆
class Person { public string name; public List<string> hobby; } void main() { Person p1 = new ...