sPRingmvc所涉及組件:
1、前段控制器DispatcherServlet,作用:接收請求,響應結果,相當于轉發器。(一般不需要程序員來開發)2、處理器映射器HandlerMapping,作用:根據請求的url查找Handler。(一般不需要程序員來開發)3、處理器Handler 編寫Handler要按照HandlerAdapter的要求去做。4、處理器適配器HandlerAdapter,作用:按照特定規則(HandlerAdapter的規則)去執行Handler。5、視圖解析器View resolver,作用:進行視圖解析,根據邏輯視圖名解析成真正的視圖。(一般不需要程序員來開發)6、視圖 View:是一個接口,實現類支持不同的View類型。springmvc相關配置:
1、在web.xml中配置如下標簽
<!--springmvc前端控制器--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--contextConfigLocation配置springmvc加載的配置文件(配置處理器、映射器、適配器等)--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-config.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!--配置url-pattern方式: 第一種:*.action 訪問以.action結尾的文件由DispatcherServlet解析 第二種:/ 所有訪問的地址都由DispatcherServlet進行解析,對于靜態文件的解析需要配置不讓DispatcherServlet進行解析, 這種方式可以實現RESTFul風格的url --> <url-pattern>*.action</url-pattern> </servlet-mapping>2、在src下會自動生成spring-config.xml,需要添加的配置如下
<!--配置Handler Handler的name屬性相當于路徑,可隨意命名,class屬性為編寫的Handler,也就是controller --> <bean name="/test.action" id="TestController"class="controller.TestController"/>
以下是兩種非注解的映射器配置
第一種配法 <!--非注解處理器映射器--> <beanclass="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
第二種配法
<!--簡單url映射,上面的第二種形式-->
<beanclass="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<propertyname="mappings">
<props>
<!--對controller進行映射-->
<prop key="/test1.action">TestController</prop>
</props>
</property>
</bean>
以下是兩種非注解的適配器配置 <!--非注解處理器適配器 要求所編寫的Handler實現Controller接口-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--另一種非注解的適配器 要求所編寫的Handler實現HttpRequestHandler接口-->
<beanclass="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
上面都是雞肋,不常用。
<!--注解映射器-->
<beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!--注解適配器-->
<beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!--使用下面的這個標簽可以代替上面的兩個注解映射器與適配器,并且封裝了很多有用的東西。實際開發就用它-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--視圖解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--這里配置前綴和后綴以后,就不用在Handler中寫絕對路徑了,只需寫上缺的那部分即可--> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean>
3、還需創建視圖,也就是jsp。
4、自行編寫Handler,也就是controller
public class TestController implements Controller { @Override public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception { //因為沒有連接數據庫,這里做數據模擬 List<UserEntity> UserList=new ArrayList<UserEntity>(); UserEntity zhangsan=new UserEntity(); zhangsan.setName("張三"); zhangsan.setSex("boy"); zhangsan.setYear(20); UserEntity lisi=new UserEntity(); lisi.setName("李四"); lisi.setYear(20); lisi.setSex("girl"); UserList.add(zhangsan); UserList.add(lisi); //返回ModelAndView ModelAndView modelAndView=new ModelAndView(); //此方法相當于request的setAttribut,這樣就可以在jsp頁面通過UserList取數據了 modelAndView.addObject("UserList",UserList); //指定視圖 modelAndView.setViewName("/WEB-INF/jsp/test.jsp"); return modelAndView; //To change body of implemented methods use File | Settings | File Templates. }}實際開發用它,上面的類沒什么用,他是非注解映射器用的@Controllerpublic class TestController03 { //@RequestMapping實現對selectAll方法和url進行映射,一個方法對應一個url //一般建議將方法名稱和url寫成一樣 @RequestMapping("/selectAll") public ModelAndView selectAll(){ //因為沒有連接數據庫,這里做數據模擬 List<UserEntity> UserList=new ArrayList<UserEntity>(); UserEntity zhangsan=new UserEntity(); zhangsan.setName("張三"); zhangsan.setSex("boy"); zhangsan.setYear(20); UserEntity lisi=new UserEntity(); lisi.setName("李四"); lisi.setYear(20); lisi.setSex("girl"); UserList.add(zhangsan); UserList.add(lisi); //返回ModelAndView ModelAndView modelAndView=new ModelAndView(); //此方法相當于request的setAttribut,這樣就可以在jsp頁面通過UserList取數據了 modelAndView.addObject("UserList",UserList); //指定視圖 modelAndView.setViewName("test"); return modelAndView; }}