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

一、配置spring mvc

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


view source print ?
01. <servlet>
02. <servlet-name>MVCservlet-name>
03. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
04. <init-param>
05. <param-name>contextConfigLocationparam-name>
06. <param-value>/WEB-INF/mvc-config.xmlparam-value>
07. init-param>
08. servlet>
09. <servlet-mapping>
10. <servlet-name>MVCservlet-name>
11. <url-pattern>*.dourl-pattern>
12. servlet-mapping>

2、mvc-config.xml文件配置

 

view source print ?
01. "1.0" encoding="UTF-8"?>
02. "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. package="cn.ecgonline.eis.controller." />
16.
17.
18.
20.
21.
22. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
25. "viewClass"
26. value="org.springframework.web.servlet.view.JstlView" />
27.
28.

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

我用的是annotation方式

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

控制器类:


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

@RequestParam 注释表示 单个属性

jsp页面代码:


view source print ?
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.
08. <html>
09. <head>
10. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
11. <title>Insert title heretitle>
12. head>
13. <body>
14. <form action="<%=basePath%>/new_workstation.do" method="post">
15. <input type="text" name="username">
16. <input type="submit" value="测试">
17. form>
18. body>
19. html>

二、后台用model类获取数据


Model类:


 

view source print ?
01. package test;
02. public class User
03. {
04. private String username;
06. public String getUsername()
07. {
08. return username;
09. }
10. public void setUsername(String username)
11. {
12. this.username = username;
13. }
15. {
16. return password;
17. }
18. public void setPassword(String password)
19. {
20. this.password = password;
21. }
22. }

控制器类:

 

view source print ?
01. package cn.ecgonline.eis.controller.workstation;
02. import javax.annotation.Resource;
03. import org.springframework.stereotype.Controller;
04. import org.springframework.web.bind.annotation.ModelAttribute;
05. import org.springframework.web.bind.annotation.RequestMapping;
06. import org.springframework.web.servlet.ModelAndView;
07. /**
08. * 测试
09. * @author 陈文龙
10. * @date 2013-9-13 下午12:49:53
11. */
12. @Controller
13. public class WrokstationController
14. {
15. @RequestMapping("/new_workstation.do")
16. public ModelAndView newWorkStation(@ModelAttribute User 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. return mv;
22. }
23. }

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

jsp页面代码:


view source print ?
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.
08. <html>
09. <head>
10. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
11. <title>Insert title heretitle>
12. head>
13. <body>
14. <form action="<%=basePath%>/new_workstation.do" method="post">
15. <table>
16. <tr>
17. <th>用户名:th>
18. <td><input type="text" name="username">td>
19. tr>
20. <tr>
21. <th>密码th>
22. <td><input type="text" name="password">td>
23. tr>
24. table>
25. <input type="submit" value="测试">
26. form>
27. body>

你可能感兴趣的:(Spring MVC前台属性数据的传递和后台属性数据的接收)