其他分享
首页 > 其他分享> > Sping——MVC(参数传递练习)

Sping——MVC(参数传递练习)

作者:互联网

需求说明:

实现View到Controller的参数传递
1、在index.jsp,输入用户编码
2、点击提交按钮,页面跳转到success.jsp页面,并在该页面输出用户提交的编码
3、要求在控制台输出从前台获取的用户编码的值

⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
代码实例:

导入相关架包:

新建index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
//创建form表单 
<form action="/login.do" method="post">
    账号:<input name="username" type="text" id="name">
    密码:<input name="password" type="text" id="pwd">
    <input type="submit" value="提交" >
</form>
</body>
</html>

配置springmvc.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">

    <!--自动扫面上下文包-->
    <context:component-scan base-package="cn.yyj.*"></context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>
    <mvc:default-servlet-handler></mvc:default-servlet-handler>

    <!--视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxUploadSize" value="10000000"></property>
    </bean>
</beans>

创建跳转页面success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>成功页面</title>
</head>
<body>
success
<br>
账号:${name}
密码:${pwd}
</body>
</html>

控制台测试

@RequestMapping("/login.do")
        public ModelAndView login(String username,String password){
            ModelAndView mv = new ModelAndView();
            //接收页面传递过来的数据  明明是id
            mv.addObject("pwd",password);
            mv.addObject("name",username);//添加模型数据
            System.out.println(username);
            System.out.println(password);
            //发送视图到success页面
            mv.setViewName("success");//设置视图名
            return mv;
        }

结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

标签:success,Sping,mv,参数传递,MVC,jsp,ModelAndView,password,页面
来源: https://blog.csdn.net/yangyingjian123/article/details/114980707