当前位置:编程学习 > JAVA >>

springMVC简单的实现代码(一)

springMVC可以实现rest风格的链接使用。非常方便
1.web.xml
[html] 
<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  <display-name>01_spring_mvc</display-name>  
    
  <!-- springMVC 1.创建servlet -->  
  <servlet>  
    <!-- 需要在WEB-INF下面创建对应的hello-servlet.xml -->  
    <servlet-name>hello</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>hello</servlet-name>  
    <!-- 捕获所有请求,基于rest -->  
    <url-pattern>/</url-pattern>  
  </servlet-mapping>  
    
  <welcome-file-list>  
    <welcome-file>index.html</welcome-file>  
    <welcome-file>index.htm</welcome-file>  
    <welcome-file>index.jsp</welcome-file>  
    <welcome-file>default.html</welcome-file>  
    <welcome-file>default.htm</welcome-file>  
    <welcome-file>default.jsp</welcome-file>  
  </welcome-file-list>  
</web-app>  
 
2.hello-servlet.xml
[html]  
<?xml version="1.0" encoding="UTF-8"?>  
<!-- springMVC 2.创建对应的servlet -->  
<beans xmlns="http://www.springframework.org/schema/beans"   
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
          
    <!-- 开启SpringMVC -->  
    <mvc:annotation-driven/>      
    <context:component-scan base-package="cn.edu.zttc.controller"></context:component-scan>  
      
    <!-- 配置视图的前缀和后缀 -->  
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/jsp/"></property>  
        <property name="suffix" value=".jsp"></property>  
    </bean>  
</beans>  
3.hello.jsp
[html]  
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
    pageEncoding="ISO-8859-1"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
<title>hello</title>  
</head>  
<body>  
hello  
<br>  
${hello}  
<br>  
${string}  
</body>  
</html>  
 
4.HelloController.java
[java]  
package cn.edu.zttc.controller;  
  
import java.util.Map;  
  
import javax.servlet.http.HttpServletRequest;  
  
import org.springframework.stereotype.Controller;  
import org.springframework.ui.Model;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
  
/** 
 * 基于@的MVC处理 
 * @author admin 
 * 
 */  
@Controller  
public class HelloController {  
  
    /** 
     * SpringMVC 
     * 设定访问规则:@RequestMapping(value={"/","/hello"}) 
     * 传递参数三种方法: 
     * 1.@RequestParam("userId") int id 
     * 2.@RequestParam() int id 
     * 3.int id 
     * 如果设置了@RequestMapping;则访问的时候必须加入?和参数,否则不会访问当前方法 
     *  
     * Struts2的 
     *  
     * @param id 
     * @return 
     */  
    @RequestMapping(value={"/","/hello"})  
    public String hello(@RequestParam("userId") int id,Map<String, Object> map){  
        System.out.println("hello");  
        System.out.println(id);  
        map.put("hello", "hello world");  
        return "hello";  
    }  
      
    /** 
     * 使用Model 
     * @param model 
     * @return 
     */  
//  public String say(@RequestParam()int id,Model model){  
    @RequestMapping(value="/say")  
   
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,