一:基于xml
的配置方式1 在web.xml中添加如下配置
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--
设置加载的springMVC 配置文件 默认文件在WEB-INF下[servletName-servlet.xml] -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>通常会用监听器来监听启动spring
的父容器application-context.xml ,父上下文容器不能访问子上下文容器中内容,
事务的Bean在父上下文容器中,无法访问子上下文容器中内容,就无法对子上下文容器中Action进行AOP(事务)
2
springmvc.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: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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" >
<!-- MVC 配置
-->
<bean
id="myaction" class="com.action.MyAction">
<!-- <property
name="commandClass">
<value>com.vo.User</value>
</property> -->
</bean>
<bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property
name="mappings">
<props>
<prop
key="start.do">myaction</prop>
<prop
key="what.do">myaction</prop>
<prop
key="login.do">myaction</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property
name="prefix"><value>/WEB-INF/jsp/</value></property>
<property
name="suffix"><value>.jsp</value></property>
<property name="viewClass">
<value>
org.springframework.web.servlet.view.JstlView
</value>
</property>
</bean>
</beans>3 action 层的配置内容:
public class
MyAction extends AbstractController{
@Override
protected ModelAndView
handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1)
throws Exception {
System.out.println("sdfas");
//WebApplicationContextUtils.getWebApplicationContext(ServletContext);
return null;
}
}二:
基于注解的spring mvc 配置:1
web.xml 中的配置不变2
springmvc-annocation.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- MVC 配置
-->
<!-- 自动扫描的包名
-->
<context:component-scan
base-package="com"></context:component-scan>
<!--
默认的注解映射的支持 -->
<context:annotation-config
/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property
name="prefix"><value>/WEB-INF/jsp/</value></property>
<property
name="suffix"><value>.jsp</value></property>
<property name="viewClass">
<value>
org.springframework.web.servlet.view.JstlView
</value>
</property>
</bean>
</beans>3 action 中的配置:
@Scope("prototype")//设置action
类采用原型,避免单例模式
@Controller
public class
ActionAnnotation {
//@Autowired//@Autowired是基于对象类型注入
@Resource(name="test")//@Resource是基于对象名称注入
private ServiceTest
st;
@RequestMapping("/login.htm")
public String
testAnnotation(){
System.out.println("adf");
st.service();
return null;
}
public ServiceTest getSt()
{
return st;
}
public void
setSt(ServiceTest st) {
this.st = st;
}
}
三:备注
<context:component-scan/>
扫描指定的包中的类上的注解,常用的注解有:
@Controller 声明Action组件
@Service
声明Service组件 @Service("myMovieLister")
@Repository 声明Dao组件
@Component
泛指组件, 当不好归类时.
@RequestMapping("/menu")
请求映射
@Resource 用于注入,(
j2ee提供的 ) 默认按名称装配,@Resource(name="beanName")
@Autowired
用于注入,(srping提供的) 默认按类型装配
@Transactional( rollbackFor={Exception.class})
事务管理
@ResponseBody
@Scope("prototype")
设定bean的作用域