數據加載中……
          sping mvc demo1
          2008年4月11日  edited by dingdangxiaoma
          spring mvc 與struts jsf 類似。表現層,因為前幾天看例子,是spring mvc寫的,所以就做個小例子吧。
          參考資料:
          http://www.ideawu.net/person/spring_mvc.html ,這是個入門的例子,但是新手做起來,真麻煩,代碼前面帶編號,真是個問題。
          能過輸入:http://localhost/springmvc1/hello.do  調用 hello.jsp 打印出Hello World 頁面。
          1.包配置:spring.jar ,common-logging.jar jstl.jar standard.jar
          2.后臺類書寫:HelloController.java
          package com.ideawu;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import org.springframework.web.servlet.ModelAndView;
          import org.springframework.web.servlet.mvc.Controller;
          public class HelloController implements Controller {
              
          /*
               * private HelloManager helloManager;
               * 
               * public void setHelloManager(HelloManager helloManager) {
               * this.helloManager = helloManager; }
               
          */
              
          public ModelAndView handleRequest(HttpServletRequest request,
                      HttpServletResponse response) 
          throws Exception {
                  request.setAttribute(
          "hello_1""你好啊, Spring!");
                  request.setAttribute(
          "hello_2""Hello World!");
                  
          return new ModelAndView("hello");
              }

          }
          3.xml配置:  ideawu-servlet.xml
          <?xml version="1.0" encoding="UTF-8" ?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
          <beans>
              
          <bean id="viewResolver"
                  
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                  
          <property name="prefix" value="/WEB-INF/jsp/" />
                  
          <property name="suffix" value=".jsp" />
              
          </bean>
              
          <bean id="simpleUrlHandlerMapping"
                  
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                  
          <property name="mappings">
                      
          <props>
                          
          <prop key="/hello.do">helloController</prop>
                      
          </props>
                  
          </property>
              
          </bean>
              
          <bean id="helloController" class="com.ideawu.HelloController">
                  
          <!--
                      
          <property name="helloManager" ref="helloManager" />
                  
          -->
              
          </bean>
          </beans>
          4.jsp 文件: /WEB-INF/jsp/hello.jsp
          <%@ page contentType="text/html; charset=UTF-8"%>
          <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
              
          <head>
                  
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                  
          <title>Hello World!</title>
              
          </head>
              
          <body>
                  
          <h2>
                      ${hello_1}
                  
          </h2>
                  
          <h2>
                      ${hello_2}
                  
          </h2>
              
          </body>
          </html>
          5.web.xml配置:
          <filter>
                  
          <filter-name>encodingFilter</filter-name>
                  
          <filter-class>
                      org.springframework.web.filter.CharacterEncodingFilter
                  
          </filter-class>
                  
          <init-param>
                      
          <param-name>encoding</param-name>
                      
          <param-value>UTF-8</param-value>
                  
          </init-param>
              
          </filter>
              
          <filter-mapping>
                  
          <filter-name>encodingFilter</filter-name>
                  
          <url-pattern>*.do</url-pattern>
              
          </filter-mapping>
              
          <servlet>
                  
          <servlet-name>ideawu</servlet-name>
                  
          <servlet-class>
                      org.springframework.web.servlet.DispatcherServlet
                  
          </servlet-class>
                  
          <load-on-startup>1</load-on-startup>
              
          </servlet>
              
          <servlet-mapping>
                  
          <servlet-name>ideawu</servlet-name>
                  
          <url-pattern>*.do</url-pattern>
              
          </servlet-mapping>
              
          <welcome-file-list>
                  
          <welcome-file>index.jsp</welcome-file>
                  
          <welcome-file>index.html</welcome-file>
              
          </welcome-file-list>
              
          <jsp-config>
                  
          <taglib>
                      
          <taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
                      <taglib-location>/WEB-INF/tld/c.tld</taglib-location>
                  
          </taglib>
                  
          <taglib>
                      
          <taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri>
                      <taglib-location>/WEB-INF/tld/fmt.tld</taglib-location>
                  
          </taglib>
              
          </jsp-config>
          以上就是程序的所代碼了??傊?,就是在web.xml中進行過慮器配置,寫后臺處理程序,xml中進行后臺處理的映射,編寫.jsp頁面。也就通過訪問*.do(或其它格式),來映射到相應的.jsp程序上。

          posted on 2008-04-11 10:26 叮當小馬 閱讀(446) 評論(0)  編輯  收藏 所屬分類: Spring


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 双江| 商都县| 雅江县| 孟州市| 石景山区| 绵竹市| 榆社县| 广西| 寿阳县| 白城市| 高邮市| 石狮市| 贵阳市| 衡阳县| 五家渠市| 嘉义市| 临安市| 岳普湖县| 喀什市| 台江县| 乌拉特中旗| 黎川县| 永清县| 临城县| 开江县| 财经| 古田县| 普定县| 新民市| 凯里市| 花莲市| 北宁市| 大余县| 兴义市| 达孜县| 芦山县| 祁东县| 淮南市| 集贤县| 文成县| 徐水县|