SpringMVC配置



1.spring-servlet.xml spring的配置文件


<?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:jee="http://www.springframework.org/schema/jee"

	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"

	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:cache="http://www.springframework.org/schema/cache"

	xsi:schemaLocation="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.0.xsd

                        http://www.springframework.org/schema/jee

                        http://www.springframework.org/schema/jee/spring-jee-4.1.xsd

                        http://www.springframework.org/schema/mvc

                        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd

                        http://www.springframework.org/schema/util 

                        http://www.springframework.org/schema/util/spring-util-4.1.xsd
                        http://www.springframework.org/schema/cache 
                        http://www.springframework.org/schema/cache/spring-cache.xsd">





	<context:annotation-config />

	<context:component-scan base-package="com.springcxf.controller" />

	<mvc:annotation-driven />



	<mvc:resources mapping="/styles/**" location="/styles/" />

	<mvc:resources mapping="/scripts/**" location="/scripts/" />

	<mvc:resources mapping="/images/**" location="/images/" />



	<bean

		class="org.springframework.web.servlet.view.InternalResourceViewResolver">

		<property name="contentType" value="text/html;charset=utf-8"></property>
		<property name="prefix" value="/WEB-INF/views/" />

		<property name="suffix" value=".jsp" />

	</bean>
	<mvc:default-servlet-handler />

</beans>


2.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_2_5.xsd"

	id="study" version="2.5">
	<display-name>Archetype Created Web Application</display-name>

	<description>sprintMVC环境搭建</description>

	<!-- 加载Spring配置文件 -->

	<context-param>

		<param-name>contextConfigLocation</param-name>

		<param-value>classpath:/configs/applicationContext-client.xml</param-value>

	</context-param>

	<!-- Spring监听 -->

	<listener>

		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

	</listener>
	
	
	
	<filter>
		<filter-name>springUtf8Encoding</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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>springUtf8Encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	

	<!-- Spring MVC配置 -->

	<servlet>

		<servlet-name>Dispatcher</servlet-name>

		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

		<!-- 自定义spring mvc的配置文件名称和路径 -->

		<init-param>

			<param-name>contextConfigLocation</param-name>

			<param-value>classpath:configs/spring-servlet.xml</param-value>

		</init-param>

		<load-on-startup>1</load-on-startup>

	</servlet>

	<!-- spring mvc 请求后缀 -->

	<servlet-mapping>

		<servlet-name>Dispatcher</servlet-name>

		<url-pattern>/</url-pattern>

	</servlet-mapping>


	
	<welcome-file-list>

		<welcome-file>index.jsp</welcome-file>

	</welcome-file-list>


</web-app>


3.使用

@Controller
public class LoginController {
    
    @RequestMapping("/login.do")//当拦截到"/login.do"执行下面的函数
    public String doLogin(HttpServletRequest request,HttpServletResponse response)
    {        
        ModelAndView mv=new ModelAndView();//model对象
        mv.addObject("arg0","hello,world!");//在model添加对象
        request.setAttribute("mv", mv);//用request来传递modelandview对象
        return "forward:/toShow";//跳转
    }
    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC是一种基于Java的Web框架,用于开发灵活、高效的Web应用程序。下面是Spring MVC的配置介绍: 1. DispatcherServlet配置:在web.xml文件中配置DispatcherServlet,它是Spring MVC的核心组件,负责接收所有的HTTP请求并将其分发给相应的处理器。 2. HandlerMapping配置:HandlerMapping负责将请求映射到相应的处理器(Controller)上。可以使用注解方式或XML配置方式进行HandlerMapping的配置。 3. Controller配置:Controller是处理请求的组件,负责处理具体的业务逻辑。可以使用注解方式或XML配置方式进行Controller的配置。 4. ViewResolver配置:ViewResolver负责将处理结果渲染为具体的视图。它根据视图名称解析出对应的视图对象,并返回给DispatcherServlet进行渲染。 5. 数据绑定和验证配置:Spring MVC提供了数据绑定和验证的功能,可以将请求参数绑定到Controller方法的参数上,并进行数据验证。可以使用注解方式或XML配置方式进行数据绑定和验证的配置。 6. 异常处理配置:Spring MVC提供了异常处理机制,可以对Controller方法中抛出的异常进行统一处理。可以使用注解方式或XML配置方式进行异常处理的配置。 7. 静态资源配置:Spring MVC可以配置静态资源的访问路径,例如图片、CSS、JavaScript等。可以使用注解方式或XML配置方式进行静态资源的配置。 8. 拦截器配置:拦截器可以在请求处理之前或之后进行一些额外的处理,例如权限验证、日志记录等。可以使用注解方式或XML配置方式进行拦截器的配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值