SpringMvc学习(1) springmvc的基本配置

1.springmvc基本配置

   导入jar包

   配置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">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>Login.jsp</welcome-file>
  </welcome-file-list>
  <!-- 前端控制器 -->
  <servlet>
  <servlet-name>springm</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:sprinmvc-servlet.xml</param-value>
  </init-param>
  </servlet>
  <servlet-mapping>
     <servlet-name>springm</servlet-name>
     <url-pattern>*.do</url-pattern>
  </servlet-mapping>
    <!-- 过滤器的配置 -->
  <filter>
  	<filter-name>encodingFilter</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</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>
</web-app>
其中,配置前端控制器,关联springmvc-servlet.xml文件,设置拦截请求;监听器可配置也可不配置,用于拦截所有页面,处理编码,防止中文乱码,统一改成utf-8编码。

   配置springmvc-servlet.xml文件 :配置Springmvc自动扫描器,视图解析器

<?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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
   <!-- 配置处理器 -->
   <!--  <bean id="login" name="/login.do" class="com.controller.LoginController"></bean>-->
   <!-- 配置普通类 -->
   <!--  <bean id="annotationLogin" class="com.controller.AnnotationLoginController"></bean>-->
   <!-- 注册注解驱动 -->
    <!--  <mvc:annotation-driven/>-->

    <!-- <mvc:annotation-driven/> -->
   <!-- 配置springmvc的自动扫描器 -->
   
   <context:component-scan base-package="com.controller"/>
   <!-- 配置视图解析器 -->
   	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   	  <!-- 前缀 -->
   	  <property name="prefix" value="/WEB-INF/jsp/"></property>
   	  <!-- 后缀 -->
   	  <property name="suffix" value=".jsp"></property>
   	</bean>
   
  </beans>
   注意:配置了自动扫描器,就不需要配置处理器和普通类了,自动扫描器会自动扫描包下的所有类,识别携带注解的:使用时,将包下的类用注解声明为Controller  注意路径匹配
@Controller 
public class HelloWordController {
	
	@RequestMapping("/helloword") 
	public String helloword(Model model){
		model.addAttribute("message","StringMvc 来了");
		return "helloword";
	}

return 返回的页面名,通过视图解析器进行路径匹配。

这样,springmvc简单的配置就完成了。可以通过前端页面进行一个简单的跳转实验

<a href="helloword.do">springmvc</a>




猜你喜欢

转载自blog.csdn.net/ke_new/article/details/78700531