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

一、配置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"?>
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值