一、概述。
在很多企业的开法中常常用到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。
相关
文章安卓 | V0.9.54.529
简体中文 | 1.6 GB
8.3
安卓 | v1.0
简体中文 | 22.8 MB
8.2
安卓 | V3.1.3
简体中文 | 121.6 MB
9.0
安卓 | v1.0.56
简体中文 | 296 MB
7.0
安卓 | V1.0.1
简体中文 | 0 Bytes
8.7
安卓 | v5.0.81
简体中文 | 667.2 MB
9.9
安卓 | V1.0.0
简体中文 | 55.4 MB
7.7
安卓 | V2023178.0.35513
简体中文 | 522.2 MB
9.5
本站为非营利性个人网站,本站所有游戏来自于互联网,版权属原著所有。如有侵犯你的版权,请发邮件给wishdown2023@163.com
湘ICP备2021017279号-3 公安部备案号:13092602000101
Copyright©2006-2019 www.wishdown.com All rights reserved