注解驱动的springMVC与参数绑定

最近一直在学习spring MVC。真的很灵活,这次主要做个前台传入数据到控制器并调用服务层来完成数据验证工作并跳转页面。

  • 工程目录结构

这里写图片描述

这次由于没有连接数据库,所以没有设置dao层。直接在service里面做了验证处理。
需求是从前台传入username和password两个参数到后台验证过完成页面跳转。
首先配置web容器的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <!-- 配置spring servlet -->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:springMVC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
    <welcome-file>welcome.jsp</welcome-file>
    </welcome-file-list>
</web-app>

然后配置springMVC的servlet。这个必须放在根包下面,之前放到包下面,总是无法起作用。
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: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-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <!-- 控制器包扫描 -->
    <context:component-scan base-package="controller"></context:component-scan>
    <!-- 视图层解析 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

然后我们需要写控制器,完成页面跳转与数据存储和业务调用:
@RequestParam(“username”)为前端的username参数放到name对象上。password同理,十分的优雅。

@Controller
@RequestMapping("/login")
public class LoginAction {
    @RequestMapping("/check")
    public ModelAndView checkLogin(@RequestParam("username") String name,@RequestParam("password") String password)
    {
        if(CheckUser.CheckUser(name, password)) return new ModelAndView("success","loginUser","hello micro");
        return new ModelAndView("error","loginName","you are not allowed into system!");
    }
}

调用的CheckUser静态构造方法为:

public class CheckUser {
    /*
     * 模拟dao完成验证
     */
    public static boolean CheckUser(String username,String password)
    {
        if(username.equals("micro") && password.equals("123")) return true;
        return false;

    }
}

完成业务层的处理。

然后来写welcome.jsp登录页面吧:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>welcome page</title>
</head>
<body>
    <h1>欢迎</h1>
    <form action="login/check" method="post">
        username:<input type="text" name="username" value="micro" /> 
        password:<input type="text" name="password" value="123" />
        <input type = "submit"  value = "提交">
    </form>
</body>
</html>

如果验证成功跳转到:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>success page</title>
</head>
<body>
<h1>this is a success page</h1>
${loginUser}
</body>
</html>

验证失败:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>error page</title>
</head>
<body>
<h1>this is a error page</h1>
${loginName}
</body>
</html>

这里写图片描述
提交就会请求loginAction,然后调用CheckUser完成业务处理 页面跳转和数据存储。
登录成功跳转:
这里写图片描述
验证失败跳转:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值