<!--在web.xml文件配置DispatcherServlet-->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>day11_springmvc</display-name>
<!-- 前端处理器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载springmvc.xml文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<!-- url-pattern匹配路径规则:
1 *.后缀名:用来匹配特定后缀名的url,如*.do,*.htm,*.action
2 /: 用来匹配除了.jsp以外的其他所有url
3 /xxx/*.后缀:用来匹配某个特定路径下相应后缀名的url
注意:不能使用/*,因为会匹配.jsp后缀,被误认为是控制器-->
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
<?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:context="http://www.springframework.org/schema/context"
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">
<!--注意:使用Springmvc注解时,必须同时使用IOC注解来创建Controller对像-->
<context:component-scan base-package="controller"></context:component-scan>
<!-- HandlerMapping处理器映射器:支持springmvc注解-->
<!--<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>-->
<!-- HandlerAdaptor处理器适配器:支持springmvc注解 -->
<!--<bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>-->
<!--上两个简写-->
<!-- Springmvc注解驱动:会创建RequestMappingHandlerMapping和RequestMappingHandlerAdapter对像-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- ViewResolver视图解析器 index逻辑视图名:/WEB-INF/jsp/index.jsp真正的视图 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
//使用注解方式时,普通的类都可以作为Springmvc的控制器
@Controller
//一般这里配置模块名
@RequestMapping("/user")
public class IndexController3 {
//进一步窄化请求
@RequestMapping("/dobiz.do")
public ModelAndView doBiz(){
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
mv.addObject("username", "admin");
System.out.println("doBiz()方法被访问");
return mv;
}
@RequestMapping("/dobiz2.do")
public ModelAndView doBiz2(){
ModelAndView mv = new ModelAndView();
mv.setViewName("index");
mv.addObject("username", "admin2");
System.out.println("doBiz2()方法被访问");
return mv;
}
}