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;
} }
项目结构:

springMVC访问 WEB-INF 下的 jsp 和 html的更多相关文章
- 【servlet】客户端是否可以访问到WEB-INF下的jsp文件
一般情况下(不考虑出现安全问题被入侵,那样啥都能访问到),WEB-INF下的jsp文件单凭浏览器端请求时访问不到的. 想访问的话需要通过服务端servlet的转发. 下面通过转发和重定向的尝试来观察访 ...
- springmvc 拦截器,不拦截jsp文件
spring mvc的拦截器只拦截controller不拦截jsp文件,如果不拦截jsp文件也会给系统带安全性问题. 解决方案有两种: 1.将所有的jsp文件放入到WEB-INF文件夹下,这样用户是直 ...
- 记录-springMVC访问web-inf下文件问题+在jsp页面导入jquery插件路径不对问题
环境:spring + springMvc + mybatis + maven 关于在springMVC环境访问web-inf目录下文件,其一有在springMVC xml文件下加 <!-- 对 ...
- springMVC框架访问web-inf下的jsp文件
博客原文章:http://td.xue163.com/1042/1/10425265.html 用户提出问题:springMVC框架访问web-inf下的jsp文件,具体如下: 使用springMVC ...
- 关于springMVC框架访问web-inf下的jsp文件
问题:springMVC框架访问web-inf下的jsp文件,具体如下: 使用springMVC,一般都会使用springMVC的视图解析器,大概会这样配置 <property name=&qu ...
- 访问WEB-INF下的jsp页面
访问web-inf下的jsp文件, 1)使用springMVC,一般都会使用springMVC的视图解析器,大概会这样配置 <!--jsp视图解析器--> <bean class ...
- 关于Spring MVC 中地址栏访问 /WEB-INF下的.jsp
WEB-INF是对资源的保护,直接在地址栏访问WEB-INF目录下的页面,会显示404,关于为什么要把页面放在WEB-INF下,可以自行百度 在这里我是用SpringMVC 对WEB-INF目录下的页 ...
- java:maven中webapp下的jsp不能访问web-inf下面的bean
java:maven中webapp下的jsp不能访问web-inf下面的bean 当然 WEB-INF下面的文件是不能访问的,只能吧jsp文件放入到WEB-INF下面,然后通过配置WEB-INF下we ...
- java web中servlet、jsp、html 互相访问的路径问题
java web中servlet.jsp.html 互相访问的路径问题 在java web种经常出现 404找不到网页的错误,究其原因,一般是访问的路径不对. java web中的路径使用按我的分法可 ...
- XAMPP环境访问非Web DocumentRoot下绝对路径
假设你的XAMPP网站文档根目录在C:/xampp/apache/htdocs/下面,那么访问这个目录下的文件是很直接的. 但是有时候需要把用户上传文件指定到特殊目录,比如E盘,那么就需要用户能够访问 ...
随机推荐
- nodejs的__dirname与__filename
__filename变量获取当前模块文件的带有完整绝对路径的文件名:[包含文件名本身的绝对路径] __dirname变量获得当前文件所在目录的完整目录名.[不包含文件名本身的绝对路径] 该方法用于获取 ...
- WebGL常用数学公式
1.三角函数 坐标轴采用右手法则,沿Z轴的逆时针方向为正角度,假设原始点为p(x,y,z),a是X轴旋转到点p的角度,r是从原始点到p点的距离.用这两个变量计算出点p的坐标,等式如下: x = rco ...
- DNS服务器介绍及设置
DNS服务器是指“域名解析服务器”,而域名就是我们通常所说的“网址”.在互联网中识别和寻找不同的计算机,实际上是需要知道该计算机的IP地址才能进行访问.比如220.181.38.4,这个IP就是百度的 ...
- PHP 使用redis
<?php /*从平台获取数据库名*/ $dbname = ""; /*从环境变量里取host,port,user,pwd*/ $host = ''; $port = ''; ...
- mysql中json_replace函数的使用?通过json_replace对json对象的值进行替换
需求描述: 在看mysql中关于json的内容,通过json_replace函数可以实现对json值的替换, 在此记录下. 操作过程: 1.查看带有json数据类型的表 mysql> selec ...
- Do you want a timeout?
Do you want a timeout? You’re feeling accomplished and excited; the new features for your applicat ...
- 用Eclipse编写Android程序的代码提示功能
用Eclipse编写Android程序的代码提示功能主要是在java和xml文件中,有时候会失效,默认的提示功能有限. 1)java文件自动提示 Window->Preferences- ...
- NetBpm如何指定下一流程处理人(8)
NETBPM如何指定下一流程处理人 本着“软件以应用为本”(潘加宇老师对我的影响在这一点上很深.)的原则,我为ERP搭建了一个用NETBPM作的支持网站,想着能够看着软件得以应用,自是非常高兴. 不过 ...
- php大数除法保留精度问题
有人在群里问大数除法,要求保留精度的问题,发现普通的方法都不能保存精度,最后找了一下资料发现可以这样 这倒是个冷门知识,嗯哼
- Web负载均衡与分布式架构
参考帖子: Web负载均衡的几种实现方式 大型网站架构系列:负载均衡详解(上) DNS 原理入门 解决nginx负载均衡的session共享问题 什么是消息队列 Java应用架构的演化之路 Java ...