Springmvc

7 篇文章 0 订阅

Spring理解 
很多初学者对于spring 都不太理解,或者说为什么要用,其实都不知道具体为什么,看别人都说好,为了看起来厉害一些,也用起来。 简单一句话来说:就是用spring来管理项目代码,能够让代码更简洁,层次更清晰,项目可扩展性更好,能够让别人更快的熟悉你的项目。 话不多说。就配一次看看看。 
1.web.xml文件
 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	
	
	<!-- 定义Spring的配置的位置,可以定义多个配置文件,可以使用通配符 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:config/env/applicationContext.xml</param-value>
	</context-param>

	
	<!-- Spring MVC 配置    action映射关系-->
	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:config/spring/spring-action-config.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监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!--配置缓存清除监听器 处理由 JavaBean Introspector功能而引起的缓存泄露 -->
	<listener>
		<listener-class>
			org.springframework.web.util.IntrospectorCleanupListener
		</listener-class>
	</listener>

	<!-- request监听 -->
	<listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>

	<!--配置编码过滤 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>com.tom.util.filter.EncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


<!-- 验证码配置 -->
	<servlet>
		<servlet-name>PictureCheckCode</servlet-name>
		<servlet-class>com.luoy.servlet.PictureCheckCode</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>PictureCheckCode</servlet-name>
		<url-pattern>/PictureCheckCode</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>goWebIndex.html</welcome-file>
	</welcome-file-list>
</web-app>

在web.xml中引入Spring 配置。把.do结尾的请求都转发到spring里面。
####2.查看applicationContext.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.2.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
	<!-- 数据库设置 -->
	<import resource="classpath:config/spring/spring-data-config.xml" />
	<!-- 服务层设置 -->
	<import resource="classpath:config/spring/spring-service-config.xml" />
	
	<import resource="classpath:config/spring/spring-bean-config.xml" />
</beans>

在这个主要的Spring文件里面,针对不同的功能再细分不同的配置文件。好处就不细说了。

3. spring-action-config.xml文件##

这个文件是用来接收action的映射的。

  <?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="   
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd   
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd   
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd   
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd   
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
            http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring 
            http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-4.1.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  "

	default-lazy-init="true">
<!-- 注册注解-->
	<context:annotation-config />
	<!-- 指定包下面的文件 注册注解-->
	<context:component-scan base-package="com.luoy.*" />
<!-- 配置拦截器 拦截所有的请求 登录拦截-->
 	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**" />
			<bean class="com.luoy.interceptor.LoginInterceptor" />
		</mvc:interceptor>
	</mvc:interceptors>
	
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<!-- String 转换器 -->
				<ref bean="stringHttpMessageConverter" />
				<!-- JSON 转换器 -->
				<ref bean="jsonHttpMessageConverter" />
			</list>
		</property>
	</bean>
	<bean id="stringHttpMessageConverter"
		class="org.springframework.http.converter.StringHttpMessageConverter" />

	<bean id="jsonHttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
		<property name="objectMapper">
			<bean class="com.fasterxml.jackson.databind.ObjectMapper">
				<property name="dateFormat">
					<bean class="java.text.SimpleDateFormat">
						<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
					</bean>
				</property>
				<property name="serializationInclusion">
                  		<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
                </property>
			</bean>
		</property>
	</bean>
	
	<!-- 视图处理器  对于action返回之后的页面 映射-->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/page/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
</beans>    

在这个配置文件中,所有的请求都会自动映射到服务端,服务端通过注解映射。


//action 里面必须使用的注解, 对应的请求路径
@RequestMapping("ly/view/project/info")
@Controller
@Scope("prototype")
public class ProjectInfoAction {
   
   private static final Logger log=  Logger.getLogger(ProjectInfoAction.class);

   //注解引用其他的类
   @Autowired
   private ProjectInfoService projectInfoService;

 
   private static final String requestPath = "ly/projectInfo/";
   
   //表示引用Model中的数据 表示请求该类的每个Action前都会首先执行它
   //这里的setReqAndRes 方法引用每次的的request和response中
    @ModelAttribute  
   public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){  
       this.request = request;  
       this.response = response;  
       this.session = request.getSession();  
   }
   
   //表示引用Model中的数据 表示请求该类的每个Action前都会首先执行它
   //model 可以自定义数据
   @ModelAttribute
   public void setPageParams(String page,
                             String rows){
      if(RString.isNotBlank(page)){
         this.page = Integer.valueOf(page);
      }
      if(RString.isNotBlank(rows)){
         this.rows = Integer.valueOf(rows);
      }
   }
   
//具体的方法 也要加上注解,对应方法
   @RequestMapping("goIntroduce")
   public String goIntroduce(){
      setAttributes();
      return requestPath+"introduce";
   }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值