SpringMVC注解版前台向后台传值的两种方式介绍
时间:1970-01-01 | 作者:网络 | 阅读:150一、概述。
在很多企业的开法中常常用到SpringMVC+Spring+Hibernate(mybatis)这样的架构,SpringMVC相当于Struts是页面到Contorller直接的交互的框架也是界面把信息传输到Contorller层的一种架构,通过这个架构可以让我们把页面和Contorller层解耦,使得开发人员的分工更加明确。
二、代码演示。
1、首先配置SpringMVC环境。
1.1导入jar。

值得注意的是红色标记的commons-logging这个jar包一定得引入进去不然会报错。
1.2、xml配置文件。
web.xml
springMVC
org.springframework.web.servlet.DispatcherServlet
1
springMVC
*.spring
index.jsp
springMVC-servlet.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
2、前台界面代码。
login.jsp
username:
password:
No.jsp
No!
Ok.jsp
OK! welcome:${username}
3、Contorller层接收前台的两种方式。
方式一:
利用@RequestParam这个注解
package com.gaowei.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class Login {
//方式一
@RequestMapping("/login")
public String login(@RequestParam("username") String username,
@RequestParam("password") String password,Model model){
if (username.equals(password))
{
model.addAttribute("username", username);
return "ok.jsp";
} else {
return "no.jsp";
}
}
}
方式二:
package com.gaowei.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class Login {
@RequestMapping("/login")
public String login(String username,String password,Model model){
if (username.equals(password))
{
model.addAttribute("username", username);
return "ok.jsp";
} else {
return "no.jsp";
}
}
}
4、界面结果。
第一种传值方式:


第二种传值方式:

三、总结。
这里体现出了SpringMVC传值方式的多样性满足了开发人员的不同需求。第一种用来表单的提交。第二种用来界面间相互传值,也为了方便开发人员利用AJAX。
福利游戏
相关文章
更多精选合集
更多大家都在玩
大家都在看
更多-
- Bitget Wallet登录难题?一招解决!
- 时间:2025-05-24
-
- 全球首搭NORDIC 54芯片!雷柏VT7二代系列鼠标图赏
- 时间:2025-05-24
-
- 失业金怎么领取 失业金线上线下领取方式对比
- 时间:2025-05-24
-
- 铁威马发布全国产备份套装:24核处理器 容量可达288TB
- 时间:2025-05-24
-
- 波场怎么样
- 时间:2025-05-24
-
- 55寸电视长宽多少厘米 55寸电视厘米尺寸详解
- 时间:2025-05-24
-
- LEO根本卖不掉吗
- 时间:2025-05-24
-
- 怎么查看自己的电子邮件 快速查找个人电子邮件地址的方法
- 时间:2025-05-24