数据加载中……
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 叮当小马 阅读(414) 评论(0)  编辑  收藏 所属分类: Spring


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


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问