Spring mvc 注解方式可以避免过多的配置文件,本文主要介绍如何使用spring mvc 的注解方式。
其次是spring.xml文件配置:
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">
view层具体实现:
自我认为,注解方式虽然省了配置文件的冗余,但在初期学习的时候并不利于排错或理解。
为了让注解更容易理解,在注解的时候应遵守命名使用规范,这样可以减少不少麻烦。写的不好的地方还请指教
首先是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");
自我认为,注解方式虽然省了配置文件的冗余,但在初期学习的时候并不利于排错或理解。
为了让注解更容易理解,在注解的时候应遵守命名使用规范,这样可以减少不少麻烦。写的不好的地方还请指教