springMVC 第三個例子

1./home.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" omit-xml-declaration="yes" />
    <xsl:template match="/">
        <html>
            <head><title>Hello!</title></head>
            <body>
                <h1>My First Words</h1>
                <xsl:for-each select="wordList/word">
                    <xsl:value-of select="."/><br />
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

2./index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>   
  </head>
  <body>
   <center><input type="button" value="xslt" onclick="location.href='home.htm'"></center>
  </body>
</html>

3./WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
 <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/application-servlet.xml</param-value>
     </init-param> 
    </servlet>  
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>   
</web-app>

4./WEB-INF/application-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="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/home.htm">homeAction</prop>
            </props>
        </property>
    </bean>  
    <bean id="bundleViewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
        <property name="basename">
            <value>views</value>
        </property>
    </bean>  
    <bean id="homeAction" class="xslt.HomePageController"/>
</beans>

5./WEB-INF/classes/views_zh_TW.properties
home.class=xslt.HomePage
home.stylesheetLocation=/home.xsl
home.root=wordList

6./WEB-INF/classes/xslt/HomePage.class
package xslt;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jdom.Element;
import org.jdom.output.DOMOutputter;
import org.springframework.web.servlet.view.xslt.AbstractXsltView;
import org.w3c.dom.Node;
public class HomePage extends AbstractXsltView {
  protected Node createDomNode(
          Map model, String rootName, HttpServletRequest req, HttpServletResponse res
      ) throws Exception {    
          org.jdom.Document doc = new org.jdom.Document();
          Element root = new Element(rootName);
          doc.setRootElement(root);
          List words = (List) model.get("wordList");
          for (Iterator it = words.iterator(); it.hasNext();) {
              String nextWord = (String) it.next();
              Element e = new Element("word");
              e.setText(nextWord);
              root.addContent(e);
          }
          // convert JDOM doc to a W3C Node and return
          return new DOMOutputter().output( doc );
      }
}


7./WEB-INF/classes/xslt/HomePageController.class
package xslt;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class HomePageController extends AbstractController{
 protected ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse resp) throws Exception {
  Map map = new HashMap();
     List wordList = new ArrayList();        
     wordList.add("hello");
     wordList.add("world");       
     map.put("wordList", wordList);      
     return new ModelAndView("home", map);
 }
}

posted on 2006-08-24 15:18 software5168 阅读(601) 评论(0)  编辑  收藏 所属分类: Java學習


只有注册用户登录后才能发表评论。


网站导航:
 
<2006年8月>
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(3)

随笔分类

随笔档案

收藏夹

JAVA學習網站

綜合

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜