一、springMVC项目配置文件
1、web.xml文件全局配置
<servlet>
<servlet-name> dispatcher </servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> dispatcher </servlet-name>
<url-pattern> / </url-pattern> //表示拦截所有请求,交由springMVC后台控制器来处理
</servlet-mapping>
//<servlet>、<servlet-mapping>这两个标签都是成对配置
2、dispatcher-servlet.xml //此处文件名xxx-servlet.xml中的xxx对应的是1中设置的Servlet-name
<beans>
<bean id = "SimpleUrlHandlerMapping" class = "org.springframework.web.servlet.Handler.SimpleUrlHandlerMapping">
<property name = "mapping">
<props>
<prop key = "/hello"> HelloController </prop> // /hello路径的请求交给id为HelloController的控制器处理,此处prop的值是下一个bean的id
</props>
</property>
</bean>
<bean id = "HelloController" class ="controller.HelloController"></bean>
</beans>
3、创建实现org.springframework.web.servlet.mvc.Controller接口的类HelloController
重写方法public ModelAndView handleRequest( HttpServletRequest p1, HttpServletResponse p2 ){
ModelAndView model = new ModelAndView("insex.jsp"); //视图
model.addObject( "message", "hello world!"); //模型数据是message,内容是hello world!
return model;
}
4、index.jsp中使用el表达式显示message的内容: <h1> ${ message } </h1>