Spring MVC前台属性数据的传递和后台属性数据的接收

来源:互联网 发布:下载软件的软件哪个好 编辑:程序博客网 时间:2024/06/07 17:42

一、配置spring mvc

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


view sourceprint?
01.<servlet>
02.<servlet-name>MVC</servlet-name>
03.<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
04.<init-param>
05.<param-name>contextConfigLocation</param-name>
06.<param-value>/WEB-INF/mvc-config.xml</param-value>
07.</init-param>
08.</servlet>
09.<servlet-mapping>
10.<servlet-name>MVC</servlet-name>
11.<url-pattern>*.do</url-pattern>
12.</servlet-mapping>

2、mvc-config.xml文件配置

 

view sourceprint?
01.<?xml version="1.0"encoding="UTF-8"?>
02.<beans xmlns="http://www.springframework.org/schema/beans"
03.xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:tx="http://www.springframework.org/schema/tx"
04.xmlns:context="http://www.springframework.org/schema/context"
05.xmlns:mvc="http://www.springframework.org/schema/mvc"
06.xsi:schemaLocation="http://www.springframework.org/schema/beans  
07.http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
08.http://www.springframework.org/schema/tx  
09.http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
10.http://www.springframework.org/schema/context 
11.http://www.springframework.org/schema/context/spring-context-3.0.xsd 
12.http://www.springframework.org/schema/mvc 
13.http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
14.<!-- 自动扫描的包名 -->
15.<context:component-scan base-package="cn.ecgonline.eis.controller."/>
16.<!-- 默认的注解映射的支持 -->
17.<mvc:annotation-driven />
18.<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射
19.<beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> -->
20.<!-- 视图解释类 -->
21.<bean
22.class="org.springframework.web.servlet.view.InternalResourceViewResolver">
23.<property name="prefix"value="/WEB-INF/<a href="http://www.it165.net/pro/webjsp/" target="_blank" class="keylink">jsp</a>/" />
24.<property name="suffix"value=".<a href="http://www.it165.net/pro/webjsp/" target="_blank" class="keylink">jsp</a>" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
25.<property name="viewClass"
26.value="org.springframework.web.servlet.view.JstlView"/>
27.</bean>
28.</beans>

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

我用的是annotation方式

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

控制器类:


view sourceprint?
01.packagecn.ecgonline.eis.controller.workstation;
02.importorg.springframework.stereotype.Controller;
03.importorg.springframework.web.bind.annotation.ModelAttribute;
04.importorg.springframework.web.bind.annotation.RequestMapping;
05.importorg.springframework.web.servlet.ModelAndView;
06./**
07.* 测试
08.* @author 陈文龙
09.* @date 2013-9-13 下午12:49:53
10.*/
11.@Controller
12.publicclass WrokstationController
13.{
14.@RequestMapping("/new_workstation.do")
15.publicModelAndView newWorkStation(@RequestParamStringusername){
16.System.out.println(user.getUsername);
17.ModelAndView mv =new ModelAndView("Hello");
18.mv.addObject("test","hello spring mvc!");
19.returnmv;
20.}
21.}

@RequestParam 注释表示 单个属性

jsp页面代码:


view sourceprint?
01.<%@ page language="java" contentType="text/html; charset=utf-8"
02.pageEncoding="utf-8"%>
03.<%
04.String path = request.getContextPath();
05.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
06.%>
07.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
08.<html>
09.<head>
10.<metahttp-equiv="Content-Type"content="text/html; charset=utf-8">
11.<title>Insert title here</title>
12.</head>
13.<body>
14.<formaction="<%=basePath%>/new_workstation.do" method="post">
15.<inputtype="text"name="username"><!--username对应后台控制器中的username-->
16.<inputtype="submit"value="测试">
17.</form>
18.</body>
19.</html>

二、后台用model类获取数据


Model类:


 

view sourceprint?
01.packagetest;
02.publicclass User
03.{
04.privateString username;
05.privateString pass<a href="http://www.it165.net/edu/ebg/"target="_blank" class="keylink">word</a>;
06.publicString getUsername()
07.{
08.returnusername;
09.}
10.publicvoid setUsername(String username)
11.{
12.this.username = username;
13.}
14.publicString getPass<a href="http://www.it165.net/edu/ebg/"target="_blank" class="keylink">word</a>()
15.{
16.returnpassword;
17.}
18.publicvoid setPassword(String password)
19.{
20.this.password = password;
21.}
22.}

控制器类:

 

view sourceprint?
01.packagecn.ecgonline.eis.controller.workstation;
02.importjavax.annotation.Resource;
03.importorg.springframework.stereotype.Controller;
04.importorg.springframework.web.bind.annotation.ModelAttribute;
05.importorg.springframework.web.bind.annotation.RequestMapping;
06.importorg.springframework.web.servlet.ModelAndView;
07./**
08.* 测试
09.* @author 陈文龙
10.* @date 2013-9-13 下午12:49:53
11.*/
12.@Controller
13.publicclass WrokstationController
14.{
15.@RequestMapping("/new_workstation.do")
16.publicModelAndView newWorkStation(@ModelAttributeUser user){
17.System.out.println(user.getUsername);
18.System.out.println(user.getPassword);
19.ModelAndView mv =new ModelAndView("Hello");
20.mv.addObject("test","hello spring mvc!");
21.returnmv;
22.}
23.}

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

jsp页面代码:


view sourceprint?
01.<%@ page language="java" contentType="text/html; charset=utf-8"
02.pageEncoding="utf-8"%>
03.<%
04.String path = request.getContextPath();
05.String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
06.%>
07.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
08.<html>
09.<head>
10.<metahttp-equiv="Content-Type"content="text/html; charset=utf-8">
11.<title>Insert title here</title>
12.</head>
13.<body>
14.<formaction="<%=basePath%>/new_workstation.do" method="post">
15.<table>
16.<tr>
17.<th>用户名:</th>
18.<td><inputtype="text"name="username"></td>
19.</tr>
20.<tr>
21.<th>密码</th>
22.<td><inputtype="text"name="password"></td>
23.</tr>
24.</table>
25.<inputtype="submit"value="测试">
26.</form>
27.</body>
0 0
原创粉丝点击