zoukankan      html  css  js  c++  java
  • springMVC访问 WEB-INF 下的 jsp 和 html

    配置freemarker,记得加上jar包

    <?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:tx="http://www.springframework.org/schema/tx"
    	   xmlns:aop="http://www.springframework.org/schema/aop"
    	   xmlns:p="http://www.springframework.org/schema/p"
    	   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/tx 
    	   http://www.springframework.org/schema/tx/spring-tx.xsd 
    	   http://www.springframework.org/schema/aop 
    	   http://www.springframework.org/schema/aop/spring-aop.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/>
    	<context:component-scan base-package="com.sawshaw" use-default-filters="false">
    		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    	</context:component-scan>
    	 <!-- 视图解析器1:html视图解析器 必须先配置freemarkerConfig,注意html是没有prefix前缀属性的 -->
    	 <bean id="freemarkerConfig"
    		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    		<property name="freemarkerSettings">
    			<bean
    				class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    				<property name="properties">
    					<props>
    						<prop key="default_encoding">utf-8</prop>
    						<prop key="output_encoding">utf-8</prop>
    					</props>
    				</property>
    			</bean>
    		</property>
    		<property name="templateLoaderPath">
    			<value>/WEB-INF/views/</value>
    		</property>
    	</bean>
    	<bean id="htmlviewResolver"
    		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"
    		p:suffix=".html" p:order="0">
    		<property name="contentType" value="text/html;charset=UTF-8" />
    	</bean>
    	<!-- 视图解析器2:jsp视图解析器 -->
    	<bean id="jspviewResolver"
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    		p:prefix="/WEB-INF/views/" p:suffix=".jsp" p:order="1">
    		<property name="contentType" value="text/html;charset=UTF-8" />
    	</bean>
        <!-- <mvc:resources location="/" mapping="*.html"/> -->
    	<mvc:resources location="/pages/" mapping="/pages/*"/>
    	<mvc:resources location="/img/" mapping="/img/**"/>
    	<mvc:resources location="/css/" mapping="/css/**"/>
    	<mvc:resources location="/js/" mapping="/js/**"/>
    </beans>
    

    Controller

    package com.sawshaw.controller;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.alibaba.fastjson.JSONObject;
    
    /**
     * @author mercy
     *页面跳转功能
     */
    @Controller
    @RequestMapping("/me")
    public class PageFowardController {
    	@RequestMapping("/greeting")
    	@ResponseBody
    	public String greeting(){
    		JSONObject js=new JSONObject();
    		js.put("id", "myId");
    		js.put("content", "mycontent");
    		return js.toJSONString();
    	}
    	//转向web-inf的hello.jsp
    	@RequestMapping("/getHelloUrl")
    	public ModelAndView handleRequest( HttpServletRequest request,HttpServletResponse response){
    		ModelAndView mav = new ModelAndView("forward:/WEB-INF/views/hello.jsp");
    		return mav;
    	}
    	//不配置freemarker返回web-infd的hello.jsp,配置了返回 web-inf的hello.html
    	@RequestMapping("/getHelloUrl0")
    	public String handleRequest0( HttpServletRequest request,HttpServletResponse response){
    		return "hello";
    	}
    	//转发请求
    	@RequestMapping("/getHelloUrl1")
    	public void handleRequest1( HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
    		request.getRequestDispatcher("getHelloUrl7").forward(request, response);
    	}
    	
    	/**
    	 * @param request
    	 * 这个方法是重定向webapp下的hello.html
    	 * @param response
    	 * @return
    	 */
    	@RequestMapping("/getHelloUrl2")
    	public ModelAndView handleRequest2( HttpServletRequest request,HttpServletResponse response){
    		ModelAndView mv = new ModelAndView("redirect:/hello.html");
    	    return mv;
    	}
    	/**
    	 * @param request
    	 * 这个方法是重定向webapp下的hello.html
    	 * @param response
    	 * @return
    	 */
    	@RequestMapping("/getHelloUrl3")
    	public String handleRequest3( HttpServletRequest request,HttpServletResponse response){
    		return "redirect:/hello.html";
    	}
    	
    	/**
    	 * @param request
    	 * 这个方法是重定向webapp下的hello.html
    	 * @param response
    	 * @return
    	 */
    	@RequestMapping("/getHelloUrl4")
    	public void handleRequest4( HttpServletRequest request,HttpServletResponse response) throws IOException{
    		response.sendRedirect(request.getContextPath()+"/hello.html");
    	}
    	
    	//返回webroot的hello.jsp
    	@RequestMapping("/getHelloUrl5")
    	public void handleRequest5( HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
    		//失败
    		//request.getRequestDispatcher("hello.jsp").forward(request, response);
    		//成功
    		response.sendRedirect(request.getContextPath()+"/hello.jsp");
    	}
    	//配置freemarker后返回web-inf的hello.html,有同名的html和jsp优先返回html
    	@RequestMapping("/getHelloUrl6")
    	public ModelAndView handleRequest6( HttpServletRequest request,HttpServletResponse response){
    		ModelAndView mav = new ModelAndView("hello");
    		return mav;
    	}
    	//返回web-inf的nihao.jsp
    	@RequestMapping("/getHelloUrl7")
    	public ModelAndView handleRequest7( HttpServletRequest request,HttpServletResponse response){
    		ModelAndView mav = new ModelAndView("nihao");
    		return mav;
    	}
    	
    	@RequestMapping("/postData")
    	@ResponseBody
    	public String postData(HttpServletRequest request,HttpServletResponse response){
    		String userName=request.getParameter("userName");
    		System.out.println("userName:"+userName);
    		return userName;
    	}
    
    }
    

      

    项目结构:

     

  • 相关阅读:
    用XMLSocket获得SmartFoxServer的zone在线人数
    php mvc开发系列教程第一节 认识mvc
    使用java编写SmartFoxServer自定义安全验证登录扩展
    php mvc开发系列教程第二节 单一入口文件(路由文件)
    php mvc开发系列教程第三节 Controller 类实现
    [转]IE下对文件(图片)进行base64转换
    从TFS中的现有项目复制一份作为新项目
    在javascript中实现document.ready,实现点Export按钮后刷新页面
    SQL Server 2005 学习笔记系列文章
    How to Read XMLDocument into a SQL Sever XML field / saving XML to database or filesystem...best method?
  • 原文地址:https://www.cnblogs.com/JAYIT/p/9374447.html
Copyright © 2011-2022 走看看