一、导包,蓝色为必须的 项目架构
二、web.xml
<servlet>
<servlet-name>springmvc</servlet-name>//名字,可以自己起,但是和下面的配置文件名是关联的,配置文件是有规范的,所以这里也算是有规范的
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>//相同
<url-pattern>*.do</url-pattern>//映射的是.do后缀的地址
</servlet-mapping>
三、类文件
public class TestController extends AbstractController{ //关键是继承
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
System.out.println("springmvc");
return new ModelAndView("index");//四-->里面指定了目录位置和后缀,注意:这里括号里面的是路径,不是某个文件
}
}
四、配置文件
springmvc-servlet.xml,名字是有规范的,包名+servlet.xml
//以前总是报utf-8的转码错误,我直接复制的web.xml,然后改的内容就不抱错了
xml version="1.0" encoding="UTF-8"?>
<beans 。。。
>
<!-- 需要配置contorller(控制器),handleMapping(映射),ViewResolver(视图),interceptor(拦截器) -->
//前缀+viewName+后缀,使用默认的映射处理器来映射controller,所名字随便起
<bean id="testController" name="/hello.do" class="springmvc.TestController"/>
//controller
//前缀+viewName+后缀
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
//中,view
<property name="prefix" value="/WEB-INF/jsp/"></property>
//前
<property name="suffix" value=".jsp"></property>
//后
</bean>
//handleMapping没有写的话是默认映射,就是调用bean里面的name。如果bean很多的话,可以用,不过按习惯就行,我到是喜欢这种直接定义name的,网上反正很多,再说。
</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: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-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"