一、配置spring mvc



1、在web.xml文件中添加如下配置:



    MVC
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        /WEB-INF/mvc-config.xml
    


    MVC
    *.do






2、mvc-config.xml文件配置




    
    
    
    
    
    
    
        
        
        
    





二、新建控制器类,获取前台属性值


我用的是annotation方式


1、后台用普通数据类型获取单个属性


控制器类:


package cn.ecgonline.eis.controller.workstation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * 测试
 * @author 陈文龙
 * @date 2013-9-13 下午12:49:53
 */
@Controller
public class WrokstationController
{
    @RequestMapping("/new_workstation.do")
    public ModelAndView newWorkStation(@RequestParam Stringusername){
    System.out.println(user.getUsername);
        ModelAndView mv = new ModelAndView("Hello");
        mv.addObject("test", "hello spring mvc!");
        return mv;
    }
}



@RequestParam 注释表示 单个属性




jsp页面代码:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
   <%
   String path = request.getContextPath();
   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
   %>




Insert title here






二、后台用model类获取数据


Model类:


package test;
public class User
{
    private String username;
    private String password;
    public String getUsername()
    {
        return username;
    }
    public void setUsername(String username)
    {
        this.username = username;
    }
    public String getPassword()
    {
        return password;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
}




控制器类:



package cn.ecgonline.eis.controller.workstation;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
 * 测试
 * @author 陈文龙
 * @date 2013-9-13 下午12:49:53
 */
@Controller
public class WrokstationController
{
    @RequestMapping("/new_workstation.do")
    public ModelAndView newWorkStation(@ModelAttribute User user){
        System.out.println(user.getUsername);
        System.out.println(user.getPassword);
        ModelAndView mv = new ModelAndView("Hello");
        mv.addObject("test", "hello spring mvc!");
        return mv;
    }
}


@ModelAttribute 注解代表用模型来接收值User对象里面的属性要和jsp页面的属性想对应



jsp页面代码:



<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
   <%
   String path = request.getContextPath();
   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
   %>




Insert title here


用户名:
密码