配置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;
} }

  

项目结构:

 

springMVC访问 WEB-INF 下的 jsp 和 html的更多相关文章

  1. 【servlet】客户端是否可以访问到WEB-INF下的jsp文件

    一般情况下(不考虑出现安全问题被入侵,那样啥都能访问到),WEB-INF下的jsp文件单凭浏览器端请求时访问不到的. 想访问的话需要通过服务端servlet的转发. 下面通过转发和重定向的尝试来观察访 ...

  2. springmvc 拦截器,不拦截jsp文件

    spring mvc的拦截器只拦截controller不拦截jsp文件,如果不拦截jsp文件也会给系统带安全性问题. 解决方案有两种: 1.将所有的jsp文件放入到WEB-INF文件夹下,这样用户是直 ...

  3. 记录-springMVC访问web-inf下文件问题+在jsp页面导入jquery插件路径不对问题

    环境:spring + springMvc + mybatis + maven 关于在springMVC环境访问web-inf目录下文件,其一有在springMVC xml文件下加 <!-- 对 ...

  4. springMVC框架访问web-inf下的jsp文件

    博客原文章:http://td.xue163.com/1042/1/10425265.html 用户提出问题:springMVC框架访问web-inf下的jsp文件,具体如下: 使用springMVC ...

  5. 关于springMVC框架访问web-inf下的jsp文件

    问题:springMVC框架访问web-inf下的jsp文件,具体如下: 使用springMVC,一般都会使用springMVC的视图解析器,大概会这样配置 <property name=&qu ...

  6. 访问WEB-INF下的jsp页面

      访问web-inf下的jsp文件, 1)使用springMVC,一般都会使用springMVC的视图解析器,大概会这样配置 <!--jsp视图解析器--> <bean class ...

  7. 关于Spring MVC 中地址栏访问 /WEB-INF下的.jsp

    WEB-INF是对资源的保护,直接在地址栏访问WEB-INF目录下的页面,会显示404,关于为什么要把页面放在WEB-INF下,可以自行百度 在这里我是用SpringMVC 对WEB-INF目录下的页 ...

  8. java:maven中webapp下的jsp不能访问web-inf下面的bean

    java:maven中webapp下的jsp不能访问web-inf下面的bean 当然 WEB-INF下面的文件是不能访问的,只能吧jsp文件放入到WEB-INF下面,然后通过配置WEB-INF下we ...

  9. java web中servlet、jsp、html 互相访问的路径问题

    java web中servlet.jsp.html 互相访问的路径问题 在java web种经常出现 404找不到网页的错误,究其原因,一般是访问的路径不对. java web中的路径使用按我的分法可 ...

  10. XAMPP环境访问非Web DocumentRoot下绝对路径

    假设你的XAMPP网站文档根目录在C:/xampp/apache/htdocs/下面,那么访问这个目录下的文件是很直接的. 但是有时候需要把用户上传文件指定到特殊目录,比如E盘,那么就需要用户能够访问 ...

随机推荐

  1. level 6 - unit4 - 强调句

    强调句 强调实义动词 范围: 一般现在时/一般过去式:肯定句 方法:v.前面加do/does/did 例子: i love you --> i do love you i loved you - ...

  2. Android 虚拟键盘弹出把底部栏顶上去的解决办法

    在AndroidManifest中使用ActivityGroup的activity中加上:android:windowSoftInputMode="adjustPan"

  3. bat 变量作用域

    set answer=one if true equ true ( set answer=two echo %answer% ) echo Argument is %answer% pause

  4. MTK 时区修改

    1.修改packages/apps/Settings/res/xml-xx-xx/timezones.xml (xx-xx表示不同的语言和区域),添加下面的内容:     <!-- timezo ...

  5. c#直接调用ssis包实现Sql Server的数据导入功能

    调用ssis包实现Sql Server的数据导入功能网上已经有很多人讨论过,自己参考后也动手实现了一下,上一次笔者的项目中还用了一下这个功能.思前想后,决定还是贴一下增强记忆,高手请54. 1.直接调 ...

  6. 多线程二(GCD)代码笔记

    // // TWFXViewController.h // Demo_GCD // // Created by Lion User on 12-12-11. // Copyright (c) 2012 ...

  7. SPREAD for Windows Forms 控制输入法

    enc = System.Text.Encoding.GetEncoding("shift-jis") datamodel = CType(FpSpread1.ActiveShee ...

  8. scala中to和util操作

    // Range:to:默认步进为1 val to1 = 1 to 10 println(to1) // 定义一个不进为2的Range val to2 = 1 to 10 by 2 println(t ...

  9. 解决“Connection to https://dl-ssl.google.com refused”问题

    相信一些人刚开始搞android的安装开发环境的时候会遇到:Failed to fectch URl https://dl-ssl.google.com/android/repository/addo ...

  10. [Command] wc

    wc 命令可以打印目标文件的换行.单词和字节数.其中换行数 = 总行数 - 1,单词数则按照空格分隔的英文单词数进行统计,也就是说连续的汉字(短语.句子)都视作一个单词. NAME wc - 打印每个 ...