用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 ...
随机推荐
- mongodb多个查询语句
db.getCollection('costitems').find({"created":{"$gte":ISODate("2019-01-02T0 ...
- VMware运行时“内部错误”的解决方法
解决方法:打开虚拟机实体目录,如下:发现有两个虚拟机配置文件,一个文件大小为4KB,另一个为空.现在虚拟机默认使用为空的配置文件了. 将大小为空的虚拟机配置文件删除掉,然后将另一个配置文件重名命. 接 ...
- Python学习之三级菜单
Python经典练习题 - 三级菜单 需求: 可依次选择进入各子菜单 可从任意一层往回退到上一层 可从任意一层退出程序 示例代码: # -*- coding: utf-8 -*- menu = { ' ...
- 大数据学习(一) | 初识 Hadoop
作者: seriouszyx 首发地址:https://seriouszyx.top/ 代码均可在 Github 上找到(求Star) 最近想要了解一些前沿技术,不能一门心思眼中只有 web,因为我目 ...
- Ajax上传文件/照片时报错TypeError :Illegal invocation
问题 Ajax上传文件/照片时报错TypeError :Illegal invocation 解决 网上搜索问题,错误原因可能有以下几个,依次检查: 请求类型有误,如post请求,但在后台设置的是ge ...
- python入门基本知识
1. 什么是语言 语言是一个事物与另外一个事物沟通的介质. python则是人(程序员)与计算机沟通的介质. 2. 什么是编程 编程就是程序员将自己想要让计算机做的事情用编程语言翻译出来写到一系列的文 ...
- 2、spring boot 配置文件
配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: •application.properties •application.yml 配置文件的作用:修改SpringBoot自 ...
- PHP.TP框架下商品项目的优化3-php封装下拉框函数
php封装下拉框函数 因为在项目中会经常使用到下拉框,所以根据一个表中的数据制作下拉框函数,以便调用 //使用一个表的数据做下拉框函数 function buildSelect($tableName, ...
- User_Authentication_Personalization Model
花了一天时间实现了一个 简单的用户登录验证的小模型. 基本实现了现在 用户登录模块的绝大多数功能, 也算是 熟悉了一下系统的逻辑. 在这个小模型中, 实现了以下的基本功能 : Logging in a ...
- Spring---单例模式(Singleton)的6种实现
1.1.1 摘要 在我们日常的工作中经常需要在应用程序中保持一个唯一的实例,如:IO处理,数据库操作等,由于这些对象都要占用重要的系统资源,所以我们必须限制这些实例的创建或始终使用一个公用的实例,这就 ...