首先需要大概的说一下,我们在用servlet的时候,有一个baseServlet用来当向导,指向不同的servlet,在用spring mvc也是这样的。有一个前置控制器和后置控制器,前置控制器就是担当向导的作用,我们需要在 web.xml里进行配置,然后后置控制器是由我们自己配置的,先说说顺序,先配置前置控制器。
思路就是:
1:我要用前置控制器
2:你去和mvc.xml交谈一下,他那里有后置控制器的位置,知道到时候要到哪里去
3:我要一开始就加载
servlet-mapping就是担当一个路径的问题,这点就不用说了。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
mvc.xml要做的就是:
你去xxx路径给我找这个后置控制器。
他那里用了注解,能给你最后的目标地
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.bjsxt.controller"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
然后控制器之一:
1:Controller就是说:这是个控制器!
2:跳转路径
@Controller
public class DemoController {
@RequestMapping({"/fuck"})
public String demo1(){
return "index.jsp";
}
}
如果重复使用了一些路径,觉得麻烦,只想写中间的路径,其他的路径由其自己定义好,可以在mvc.xml文件中提前配置
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF\js\"></property>
<property name="suffix" value=".jsp"></property>
</bean>
如果需要访问静态资源的话
需要在xml文件中,自己加入
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>