Spring MVC项目配置项目主页

希望访问项目根路径的时候就跳转到spring控制的首页

一种做法是Spring处理"*.html路径",另一种做法是Spring处理“/”路径

一:Spring处理"*.html路径":

1、修改web.xml的欢迎页,手动指定欢迎页,后缀就是url-pattern里的配置,这样会把首页的访问交给spring处理

<welcome-file>index.html</welcome-file>

 

<servlet>
    <servlet-name>antsclub</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<load-on-startup>3</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>antsclub</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>
	
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

 

spring会自动去找antsclub-servlet.xml配置文件,这个文件不存在会报错

2、controller处理路径

@RequestMapping(value = "/index.html")
public String loginPage(){
    return "login";
}

 

这里的login会跳转到指定的view,view的路径和格式在antsclub-servlet.xml里面配置

<!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->
<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:viewClass="org.springframework.web.servlet.view.JstlView" 
	p:prefix="/WEB-INF/jsp/"
	p:suffix=".jsp" />

WEB-INF被称为安全目录,该目录下的文件只有服务端可以访问,客户端不能访问。

该目录下的jsp只能Forward不能Redirect。

forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服务器发送的内容是从哪儿来的,所以它的地址栏中还是原来的地址。
redirect就是服务端根据逻辑,发送一个状态码,告诉浏览器重新去请求那个地址,一般来说浏览器会用刚才请求的所有参数重新请求,地址栏会变

二、Spring处理“/”路径的步骤:

1、web.xml配置

	<servlet-mapping>
		<servlet-name>tsingyu</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

2、Controller代码:

	@RequestMapping(value = "/")
	public String index(){
		return "login";
	}

但是这样处理会有一个问题:静态资源访问不到了,报404错误。

解决办法(针对spring3.0.5适用):修改ApplicationContext-mvc.xml:

<?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: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-3.0.xsd
       http://www.springframework.org/schema/mvc 
	   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
       
    <mvc:annotation-driven/>	
    <!-- 扫描web包,应用Spring的注解 -->
	<context:component-scan base-package="cn.tsingyu.spring.example.controller"/>
	<mvc:resources mapping="/static/**" location="/static/" />
	<!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:viewClass="org.springframework.web.servlet.view.JstlView" 
		p:prefix="/WEB-INF/jsp/"
		p:suffix=".jsp" />
</beans>

其中mvc部分是新增的内容包括

xmlns:mvc="http://www.springframework.org/schema/mvc"

http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

<mvc:annotation-driven/>
<mvc:resources mapping="/static/**" location="/static/" />

附:web.xml中url-pattern匹配规则

转载于:https://my.oschina.net/u/173975/blog/671170

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值