一、配置spring mvc



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


<servlet>
    <servlet-name>MVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-config.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>MVC</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>






2、mvc-config.xml文件配置


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- 自动扫描的包名 -->
    <context:component-scan base-package="cn.ecgonline.eis.controller." />
    <!-- 默认的注解映射的支持 -->
    <mvc:annotation-driven />
    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> -->
    <!-- 视图解释类 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
    </bean>
</beans>





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


我用的是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+"/";
   %>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="<%=basePath%>/new_workstation.do" method="post">
<input type="text" name="username"><!--username对应后台控制器中的username-->
<input type="submit" value="测试">
</form>
</body>
</html>





二、后台用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+"/";
   %>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="<%=basePath%>/new_workstation.do" method="post">
    <table>
        <tr>
          <th>用户名:</th>
          <td><input type="text" name="username"></td>
        </tr>
         <tr>
          <th>密码</th>
          <td><input type="text" name="password"></td>
        </tr>
    </table>
    <input type="submit" value="测试">
</form>
</body>
</html>