1,web.xml配置-----------------------html
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>web
2,當web.xml中DispatcherServlet中沒有配置ini-param,則在web-inf下建一個文件:文件名必須爲 DispatcherServlet-servlet.xmlspring
就是 servlet-name的值 加上"-servlet.xml"mvc
或者 <servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>app
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>jsp
3.--dispatcherServlet-servlet.xml配置------------------------------------this
<!-- 註冊Action
class表明處理類的全路徑
name表示請求路徑url
此配置必寫
-->
<bean name="/hello.action" class="cn.gdpe.springmvc.HelloAction"></bean>spa
<bean id="userActionId" class="cn.gdpe.springmvc.HelloAction"></bean>xml
<!-- 映射器
表示將bean標籤的name屬性當作URL請求
可選
-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!--SimpleUrlHandlerMapping將多個請求映射到同一個Action -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/add.action">userActionId</prop>
<prop key="/delete.action">userActionId</prop>
<prop key="/update.action">userActionId</prop>
<prop key="/select.action">userActionId</prop>
</props>
</property>
</bean>
<!-- 適配器
表示用於尋找實現了controller接口的action類
可選
-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 視圖器
表示用於匹配modelAndView.setViewName("/springMvcFirst.jsp");中封裝的路徑
可選
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
<!-- 專用於jsp到jsp/html的轉發控制器 -->
<bean name="/testJsp.action" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<!-- 轉發到真實視圖名 -->
<property name="viewName" value="/forwardTestJsp.jsp"/>
</bean>
4-controller類----------------------------------------------
public class HelloAction implements Controller {
public HelloAction(){
System.out.println("HelloAction()::"+this.hashCode());
}
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
/*
* ModerAndView 表示封裝的視圖數據與路徑
*/
ModelAndView modelAndView =new ModelAndView();
modelAndView.addObject("message", "第一個springmvc應用程序");
modelAndView.setViewName("/springMvcFirst.jsp");
System.out.println("handlerRequest----->");
return modelAndView;
}
}