spring4.1.6配置MVC注解

运行系统及跳转流程


web点xml

路径为与WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app  version="3.1" 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">

    <!-- DispatcherServlet则定义了mvc的相关内容,并配置拦截的url,如上面所示,所有/开头的请求,都会通过SpringMVC这个servlet进行处理 -->
	<servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/SpringMVC-servlet.xml</param-value>
            <!--
            	实际上也可以不通过 contextConfigLocation类配置SpringMVC的配置文件,而使用默认的
            	默认是/WEB-INF/[servlet-name]-servlet.xml 
            -->
        </init-param>
        <load-on-startup>1</load-on-startup>
        <!-- 1代表启动系统是加载SpringMVC -->
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
        <!--
        	/*强迫所有的请求及响应都经过该servlet; 
			/将使你配置的servlet成为默认的servlet。 
		-->
    </servlet-mapping>
  	
  	<!-- ContextLoaderListener指定了IOC容器初始化的方法 -->
	<listener>
		<listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
	</listener>
	
    <!-- 本节没用到 -->
	<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
        <!-- 默认是/WEB-INF/applicationContext.xml -->
    </context-param>     
</web-app>

SpringMVC-servlet点xml

路径为WEB-INF/SpringMVC-servlet.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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:util="http://www.springframework.org/schema/util"
	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/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
	<!-- 配置自动扫描的包 -->
	<context:component-scan base-package="com.lt.springmvc"></context:component-scan>

	<!-- 配置mvc:resources后注解方式的url就没有加载,mvc:annotation-driven可以让注解的URL继续使用 -->
	<mvc:annotation-driven />
	<!-- 当在web.xml 中 DispatcherServlet使用 <url-pattern>/</url-pattern> 映射时,能映射静态资源 -->
	<mvc:default-servlet-handler />
	<mvc:resources mapping="/jquery/**" location="/WEB-INF/jquery/" />
	<mvc:resources mapping="/view/**" location="/WEB-INF/view/" />
	
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>





HelloController点class

位置为classpath:hello/HelloController.class

package hello;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
 /**
  * 1.使用RequestMapping注解来映射请求的URL
  * 2.返回值会通过视图解析器为实际的物理视图,对与org.springframework.web.servlet.view.InternalResourceViewResolver视图解析器,会做如下解析:
  * prefix + returnVal + suffix =物理视图位置 然后进行转发
  * @return
  */
 @RequestMapping("/helloword")
 public @ResponseBody String test() {
  return "helloword";
 }
}

helloword点jsp

位置为WEB-INF/view/helloword.jsp

需要的jar包

commons-logging-1.0.4.jar

spring-aop-4.1.6.RELEASE.jar

spring-beans-4.1.6.RELEASE.jar

spring-context-4.1.6.RELEASE.jar

spring-core-4.1.6.RELEASE.jar

spring-expression-4.1.6.RELEASE.jar

spring-web-4.1.6.RELEASE.jar

spring-webmvc-4.1.6.RELEASE.jar


当笔记用,有问题请指出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值