Spring MVC前台属性数据的传递和后台属性数据的接收
来源:互联网 发布:下载软件的软件哪个好 编辑:程序博客网 时间:2024/06/07 17:42
一、配置spring mvc
1、在web.xml文件中添加如下配置:
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文件配置
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.
<bean
class
=
"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、后台用普通数据类型获取单个属性
控制器类:
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页面代码:
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.
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=utf-8"
>
11.
<
title
>Insert title here</
title
>
12.
</
head
>
13.
<
body
>
14.
<
form
action="<%=basePath%>/new_workstation.do" method="post">
15.
<
input
type
=
"text"
name
=
"username"
>
<!--username对应后台控制器中的username-->
16.
<
input
type
=
"submit"
value
=
"测试"
>
17.
</
form
>
18.
</
body
>
19.
</
html
>
二、后台用model类获取数据
Model类:
01.
package
test;
02.
public
class
User
03.
{
04.
private
String username;
05.
private
String pass<a href=
"http://www.it165.net/edu/ebg/"
target=
"_blank"
class
=
"keylink"
>word</a>;
06.
public
String getUsername()
07.
{
08.
return
username;
09.
}
10.
public
void
setUsername(String username)
11.
{
12.
this
.username = username;
13.
}
14.
public
String getPass<a href=
"http://www.it165.net/edu/ebg/"
target=
"_blank"
class
=
"keylink"
>word</a>()
15.
{
16.
return
password;
17.
}
18.
public
void
setPassword(String password)
19.
{
20.
this
.password = password;
21.
}
22.
}
控制器类:
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页面代码:
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.
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=utf-8"
>
11.
<
title
>Insert title here</
title
>
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
>
0 0
- spring mvc 前台属性数据的传递和后台属性数据的接收
- Spring MVC前台属性数据的传递和后台属性数据的接收
- Struts2,spring前台后台的数据传递及配置解析
- 浅谈Struts2前台后台的数据传递
- 【AJAX】前台与后台的数据传递
- Spring mvc 前端表单传递来的数据被后台的两个entity的相同名字的属性谁获取的问题
- JSP_strut2架构下前台接收后台传递到前台之数据并处理以使用的一种方法
- ASP.NET中MVC从后台控制器传递数据到前台视图的方式
- Spring MVC接收前台form表单提交数据的乱码问题
- mybaits中后台接收前台提交的数据解析
- jquery ajax的data属性传递数组,后台接收
- SpringMVC 之 后台接收参数 与前台传递数据
- Spring-MVC-配置接收和返回数据的格式
- Spring MVC 的数据绑定、转换器、属性编辑器
- spring MVC 数据的绑定 验证 转换---属性编辑器
- 前台和后台的数据交互
- Spring MVC处理前台到后台绑定时间格式、doble等数据的解决方式
- springmvc 后台接收前台数据
- Virtualbox WDDM之DxgkDdiInterruptRoutine
- 编译和链接
- 坏的解释器: 没有那个文件或目录/bin/sh
- C语言C++语言中静态变量和静态函数
- REST Web应用,ZF框架 Rest CS服务实现
- Spring MVC前台属性数据的传递和后台属性数据的接收
- 增删改操作
- latex 论文致谢
- Java中创建线程的两种方式及线程的状态复习
- 在WMWare 安装Redhat Linux 6.4 basic server版本(没有图形界面)网络连接
- 获取最大值
- LeetCode——Product of Array Except Self
- java ee web开发学习笔记
- MySQL之——分布式集群