zoukankan      html  css  js  c++  java
  • spring mvc:内部资源视图解析器(注解实现)@Controller/@RequestMapping

    spring mvc:内部资源视图解析器(注解实现)@Controller/@RequestMapping

    项目访问地址:

    http://localhost:8080/guga2/hello/print

    http://localhost:8080/guga2/hello

    用到的注解类:

    org.springframework.stereotype.Controller;
    org.springframework.web.bind.annotation.RequestMapping;
    org.springframework.web.bind.annotation.RequestMethod;
    

      

    视图模型类

    org.springframework.ui.ModelMap;
    

     

    本例中,包名:springweb 

    本例中有三个xml配置文件:web.xml, applicationContext.xml,xxxx-servlet.xml

    web.xml

      <!--配置文件路径-->
    <context-param>
     	<param-name>contextConfigLocation</param-name>
    	<param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    
    <!-- 字符过滤器 -->  
    <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> 
    
    
    <!-- 监听转发 -->
    <listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
      <servlet>  
        <servlet-name>springweb</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>springweb</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping> 
    

      

    applicationContext.xml

    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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.xsd">
    
    
    <!-- 默认:注解映射支持 -->		
    <mvc:annotation-driven/>
    <!-- 静态资源配置 -->
    <mvc:resources location="/pages/**" mapping="/pages/"/>
    
    <!-- 自动扫描包名,controller -->
    <context:component-scan base-package="springweb"/>
    			
    
    
    
    </beans>
    

      

    springweb-servlet.xml

    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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.xsd">
    					
    
    <!-- 视图 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    	<property name="prefix" value="/WEB-INF/jsp/"/>
    	<property name="suffix" value=".jsp"/>
    </bean>
    
    
    </beans>
    

      

    jsp代码

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
    <%@ page isELIgnored="false" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>hello world</title>
    </head>
    <body>
    
    
    
    ${message}
    
    </body>
    </html>
    

      

    HelloController.java代码

    1.项目/hello访问代码

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.ui.ModelMap;
    
    
    @Controller
    @RequestMapping(value="/hello")
    public class HelloController {
    
    	
    	@RequestMapping( method=RequestMethod.GET)
    	public String printHello(ModelMap model)
    	{
    		model.addAttribute("message", "spring mvc framework映射网页与请求实例");
    		return "hello";
    		
    	}
    	
    }
    

      

    2.项目/hello/print访问代码

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.ui.ModelMap;
    
    
    @Controller
    public class HelloController {
    
    	
    	@RequestMapping(value="/hello/print", method=RequestMethod.GET)
    	public String printHello(ModelMap model)
    	{
    		model.addAttribute("message", "spring mvc framework映射网页与请求实例");
    		return "hello";
    		
    	}
    	
    }
    

      

  • 相关阅读:
    红线行动开发文档
    团队作业1
    第二次作业:安装VS2015和使用自动测试管理工具
    简单介绍VS2015自动测试工具
    软件工程作业(一)
    三带一队 实验十 团队作业6:团队项目用户验收&BETA冲刺
    《三带一队》【Beta】Scrum meeting 4
    《三带一队》【Beta】Scrum meeting 3
    《三带一队》【Beta】Scrum meeting 2
    《三带一队》【Beta】Scrum meeting 1
  • 原文地址:https://www.cnblogs.com/achengmu/p/8985983.html
Copyright © 2011-2022 走看看