springmvc框架

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>demo1</display-name>
  
  <!-- 注册springmvc核心控制器 -->
  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 通知DispatcherServlet去指定的目录下加载springmvc.xml配置文件 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

web.xml映射文件springmvc.xml

//springmvc.xml可以处于src文件根目录下
<?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:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
	  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/context
      http://www.springframework.org/schema/context/spring-context-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
    
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        
      ">
      
      
	//映射在text包下的springmvc-test.xml文件
	<import resource="test/springmvc-test.xml"/>
      
</beans>

springmvc-test.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:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
	  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/context
      http://www.springframework.org/schema/context/spring-context-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
    
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        
      ">
      
    <!-- 控制器(程序员) -->
    <bean name="/hello.action" class="test.HelloAction"></bean>
    
    <!-- 
    可选
    	映射器(框架) 将bean标签的name属性当作URL请求
     -->  
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>  
      
    <!--
    可选
    	适配器(框架) 寻找实现了Controller接口的Action类
    	也能去找继承了AbstractCommandController类的Action
    -->  
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>  
      
    <!-- 
    可选
    	视图解析器(框架) 通过ModelAndView对象中封装的视图名找到真正的页面
    -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
    
</beans>

springmvc-test.xml映射文件HelloAction.java

//HelloAction.java为普通类,继承Controller
package test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloAction implements Controller{
	public HelloAction(){
		System.out.println("HelloAction()::" + this.hashCode());
	}
	/**
	 * 业务方法
	 */
	public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {

		/**
		 * ModelAndView表示向视图封装的数据和真实路径
		 */
		ModelAndView modelAndView = new ModelAndView();
		
		//addObject方法设置了要传递给视图的对象
		modelAndView.addObject("message","这是我的第一个springmvc应用程序,映射器_适配器_视图解析器_都可省略");
		
		modelAndView.setViewName("/jsp/success.jsp");

		//返回ModelAndView对象会跳转至相应的视图文件。也将设置的参数同时传递至视图
		return modelAndView;
	}
}

用户可通过输入:http://localhost:8080/demo1/hello.action
可以直接跳转到我们的/jsp/success.jsp文件

视图解析器(框架) 通过ModelAndView对象中封装的视图名找到真正的页面

//原来封装视图的真实路径
//modelAndView.setViewName("/jsp/success.jsp");

//现在封装视图的逻辑名称
modelAndView.setViewName("success");

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<!-- 路径前缀 -->
    	<property name="prefix" value="/jsp/"></property>
    	<!-- 路径后缀 -->
    	<property name="suffix" value=".jsp"></property>
    	<!-- 前缀+视图逻辑名+后缀=真实路径 -->
    </bean>

注册映射器 适合于将多个请求,映射到同一个Action

    <bean id="HelloActionID" class="test.HelloAction"></bean>
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    	<property name="mappings">
    		<props>
    			<!-- key表示请求路径 -->
    			<prop key="/hello.action">HelloActionID</prop>
    			<prop key="/hello1.action">HelloActionID</prop>
    			<prop key="/hello2.action">HelloActionID</prop>
    		</props>
    	</property>
    </bean>

专用于jsp到jsp/html的转发控制器

//将直接将转发到目标地址,不需要通过Controller
<bean name="/add.action" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
	<!-- 转发到真实视图名 -->
	<property name="viewName" value="/WEB-INF/jsp/index.jsp"/>
</bean>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值