springmvc注解方式

Spring mvc 注解方式可以避免过多的配置文件,本文主要介绍如何使用spring mvc 的注解方式。

首先是web.xml文件配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <filter>
        <filter-name>CharsetFilter</filter-name>
        <filter-class>com.javaweb.filter.CharsetFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CharsetFilter</filter-name>
        <url-pattern>/charset</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>reg/login.jsp</welcome-file>
    </welcome-file-list>
        <resource-ref>
            <description>连接池</description>
            <res-ref-name>jdbc/wang</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
        </resource-ref>
    <!-- 这里在配成spring,下边也要写一个名为spring.xml的文件,主要用来配置它的controller --> 
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springX.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

其次是spring.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/aop
		 http://www.springframework.org/schema/aop/spring-aop.xsd
		 http://www.springframework.org/schema/tx
		 http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
     <!-- 开启注解 -->
     <context:annotation-config></context:annotation-config>
    <!-- 指定扫描包 -->
    <context:component-scan base-package="com.javaweb.service"/>
    <context:component-scan base-package="com.javaweb.controllers" />
    <context:component-scan base-package="com.javaweb.bean" />
     <!--开启springmvc注解-->
    <mvc:annotation-driven />
    <!-- 开启aop -->
    <aop:scoped-proxy />
    <!-- spring-mybatis配置 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/wang?useUnicode=true&characterEncoding=utf-8"/>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <!-- springmvc事务配置 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
         <tx:attributes>
             <tx:method name="*" rollback-for="java.lang.Exception"/>
         </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="pc" expression="execution(* com.javaweb.service..*(..))"></aop:pointcut>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"></aop:advisor>
    </aop:config>
</beans>



controller实现的时候只需要把加上注解,spring就会自动找到对应的bean。


@Controller、@Service 以及 @Repository 和 @Component 注解的作用是等价的:
将一个类成为 Spring 容器的 Bean,使用不同的注解只是方便读者辨认这是那一层的内容。
由于 Spring MVC 的 Controller 必须事先是一个 Bean,所以 @Controller 注解是不可缺少的。

@RequestMapping 可以标注在类定义处,将 Controller 和特定请求关联起来;还可以标注在方法名处,以便进一步对请求进行分流。
在方法名处的注解可有多种方式:
@RequestMapping("/***.do"):即在view层写的时候写为("路径/***.do")
@RequestMapping(params = "method=name") :即在view层写的时候写为<form method="post" action="<%=request.getContextPath() %>/***.do?method=save"> 

(此处的***为在类名上的注解)

@Resource(name = "fenleiService")
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
  @Resource装配顺序
  1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
  2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
  3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
  4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;

controller具体实现:

@RequestMapping("/fenlei")
public class FenleiController {
     @Autowired     //自动匹配
   @Resource(name = "fenleiService")
   private FenleiService fenleiService;
   @RequestMapping("/fenlei.do")
    public void fenlei(HttpServletRequest request, HttpServletResponse response){
	
	}
}


view层具体实现:

$(function () {
    $.ajaxSetup({
        async:false
    });
        var url = "/Task/fenlei/fenlei.do";
        data = {};
        data.flag = "all";
    $.post(url,data,function (result) {
        for(var i=0;i<result.getAll.length;i++){
            var id = result.getAll[i].fenleiId;
            var name = result.getAll[i].fenleiName;
            var newrow = "<tr id='tr"+id+"'><td>"+result.getAll[i].fenleiId+"</td><td id='td"+id+"'>"+result.getAll[i].fenleiName+
                "</td><td><button οnclick='del("+id+")''>删除</button><button οnclick='edit("+id+")'>修改</button></td></tr>"
            $("#AllInfo tr:last").after(newrow);
        }
    },"json");

自我认为,注解方式虽然省了配置文件的冗余,但在初期学习的时候并不利于排错或理解。
为了让注解更容易理解,在注解的时候应遵守命名使用规范,这样可以减少不少麻烦。写的不好的地方还请指教





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值