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。
福利游戏
相关文章
更多精选合集
更多大家都在玩
热门话题
大家都在看
更多-
- 币圈订单簿详解:提升交易决策
- 时间:2025-04-15
-
- 上海地铁全线网5G公众信号正式开通:实测下行峰值超1Gbps
- 时间:2025-04-15
-
- 移动级RTX 5080性能实测:DLSS 4封神
- 时间:2025-04-15
-
- 男子二手平台6万块买特斯拉Model S:维修费需要20万
- 时间:2025-04-15
-
- 索拉纳币暴涨潜力分析:未来价格预测
- 时间:2025-04-15
-
- 币格交易所新手攻略:买卖币种指南及注意事项
- 时间:2025-04-15
-
- 期货App排行榜:2023十大炒币平台
- 时间:2025-04-15
-
- 欧亿官网下载正版APP:全球USDT交易入口
- 时间:2025-04-15