springMVC 第一個例子

1. /hellouser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head><title>HelloPage</title></head>
<body>
     <H1> ${helloWord}</H2>
</body>
</html>

2./index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
    <%
     response.sendRedirect("hellouser.do");
    %>
  </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>*.do</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="/hellouser.do">helloUserAction</prop>
            </props>
        </property>
    </bean>
   
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.InternalResourceView</value>
        </property>
    </bean>
   
    <bean id="helloUserAction" class="onlyfun.caterpillar.HelloUserAction">
        <property name="helloWord">
            <value>Hello!</value>
        </property>
        <property name="viewPage">
            <value>/hellouser.jsp</value>
        </property>
    </bean>
</beans>

5./WEB-INF/classes/onlyfun/caterpillar/HelloUserAction.class
package onlyfun.caterpillar;

import java.io.IOException;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.RequestUtils;

public class HelloUserAction implements Controller {
   private String helloWord;
    private String viewPage;
   
   public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
     //String user = RequestUtils.getRequiredStringParameter(req, "user");
      Map model = new HashMap();
      model.put("helloWord", getHelloWord());
     // model.put("user", user);
   
       return new ModelAndView(getViewPage(), model);
    }
   
    public void setViewPage(String viewPage) {
       this.viewPage = viewPage;
    }
   
    public String getViewPage() {
       return viewPage;
    }

    public void setHelloWord(String helloWord) {
       this.helloWord = helloWord;
    }
   
    public String getHelloWord() {
       return helloWord;
    }
}

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


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


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

导航

统计

常用链接

留言簿(3)

随笔分类

随笔档案

收藏夹

JAVA學習網站

綜合

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜