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。
福利游戏
相关文章
更多精选合集
更多大家都在玩
热门话题
大家都在看
更多-
- 浪漫餐厅兑换码2.20
- 时间:2025-02-21
-
- 形容读书多学问高的成语是什么 蚂蚁庄园2月20日答案最新
- 时间:2025-02-21
-
- 巨螯蟹属现存唯一的品种是什么 神奇海洋2月19日答案最新
- 时间:2025-02-21
-
- 古钱币文创师的主要工作是仿制古钱币吗 蚂蚁新村2月19日答案
- 时间:2025-02-21
-
- 古钱币文创师的主要工作是什么 蚂蚁新村2月19日答案最新
- 时间:2025-02-21
-
- 支付宝蚂蚁森林神奇海洋2月19日答案
- 时间:2025-02-21
-
- 古钱币文创师的主要工作是仿制古钱币吗
- 时间:2025-02-21
-
- 学富五车和蝉不知雪哪个常用来形容读书多学问高
- 时间:2025-02-21