• 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";
    		
    	}
    	
    }
    

      

  • 相关阅读:
    docker in docker
    docker社区的geodata/gdal镜像dockerfile分析
    howto:在构建基于debian的docker基础镜像时,更换国内包源
    使用Visual Studio 2017构建.Net Core的Docker镜像
    步骤:asp.net core中使用identifyserver4颁发令牌
    部署:阿里云ECS部署Docker CE
    问题:调用 ASP.Net Core WebAPI的HTTP POST方法时,从 [FromBody] 中读取的 MongoDB GeoJsonObjectModel成员总是null
    《.NET 微服务:适用于容器化 .NET 应用的体系结构》关键结论
    SQL数据库注入防范 ASP.NET Globle警告
    数据库中的恶意字符批处理
  • 原文地址:https://www.cnblogs.com/achengmu/p/8985983.html
Copyright © 2020-2023  润新知