一、springmvc 配置默认路径
1.默认tomcat容器的默认页面。
/index.html
这种方式适合访问静态的页面(也包括JSP)或者说是没有任何参数的页面。
或者配置web.xml中的welcome-file-list
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>2.配置web.xml
<servlet>
<servlet-name>vanclshare</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:vanclshare-servlet.xml</param-value>
</init-param>
<!-- 启动容器时初始化Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>vanclshare</servlet-name>
<!-- 定义哪些请求交给springmvc处理,“*.html”表示默认由servlet映射,表示所有的映射都会被拦截 -->
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!-- 拦截请求 -->
<servlet-mapping>
<servlet-name>vanclshare</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
<!-- 默认欢迎页 -->
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>controller中需要配置index 的requestMapping
@Controller
public class IndexController {
@RequestMapping("/index")
public ModelAndView Index()
{
ModelAndView modelAndView = new ModelAndView("/index");
return modelAndView;
}
@RequestMapping("/index1")
public ModelAndView Index1()
{
ModelAndView modelAndView = new ModelAndView("/index1");
return modelAndView;
}
}
直接访问 http://localhost:8080/VanclShare 即可访问映射为index 的controller。
二、include跳转controller
<body>
这是我的index
<jsp:include page="/index1.html"></jsp:include>
</body>根据web.xml中映射拦截器,会拦截*.html配置的所有请求。那么会跳转到@RequestMapping("/index1") 的请求中,返回名为index1的视图
index1.jsp内如如下:
<body>
这是index1
</body>直接访问 http://localhost:8080/VanclShare 得到如下结果:
三、url
get访问中文乱码
为了防止中文视图乱码,请求乱码等,一般我们需要配置三个地方。
这里我们统一配置为UTF-8的模式
1、jsp配置编码方式,在jsp头部配置如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
这是index1
</body>
</html>2、web.xml配置字符集过滤filter
配置如下:
<!-- 配置字符集 -->
<filter>
<filter-name>CharacterEncodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>3、配置web服务器的字符集。这里以tomcat为例
找到conf下的server.xml文件,添加字符集URIEncoding=“UTF-8”
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
四、配置域名访问
第一是 <Engine defaultHost="localhost" name="Catalina"> 把defaultHost的值修改成你的域名
<Engine defaultHost="www.test.com" name="Catalina">
第二是 <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
把name的值修改成你的域名
<Host appBase="webapps" autoDeploy="true" name="www.test.com" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
www.test.com : 为申请好的域名或者可供访问的ip地址
本文详细介绍了Spring MVC如何配置默认访问路径,包括通过Tomcat默认页面和web.xml设置。此外,讨论了JSP include跳转Controller的方法,并针对URL GET访问中文出现乱码的问题,提出了解决方案,包括在jsp、web.xml和server.xml中配置UTF-8编码。

4125

被折叠的 条评论
为什么被折叠?



