【实例】使用SpringMVC添加页面,实现转账功能(xml+注解)

之前的转账案例都是 通过test测试service和dao层
今天写一个web页面,输入转账人,收款人,金额,然后点击转账按钮完成转账。
  • web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1"
         metadata-complete="false">

    <servlet>
        <servlet-name>springmvcservlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>
  • applicationContext.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: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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">

    <!--xml配置方法-->
    <bean id="advice" class="com.bamzhy.advice.NewMyAdvice"/>

    <aop:config>
        <!--aspect填写注入的实例类的id-->
        <aop:aspect ref="advice">
            <!--pointcut填写切入点属性-->
            <aop:pointcut
                    id="mypointcut"
                    expression="execution (public boolean com.bamzhy.service.UserServiceImpl.transfer(..))"/>
            <!--这里填写method名和pointcut名-->
            <aop:around method="around" pointcut-ref="mypointcut"/>
            <aop:after method="finallyMethod" pointcut-ref="mypointcut"/>
        </aop:aspect>
    </aop:config>

    <!--这里是加强方法所在类-->
    <bean id="service" class="com.bamzhy.service.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>
    <!--这个是别人需要实例化的对象,提供给别人ref使用-->
    <bean id="userDao" class="com.bamzhy.dao.UserDaoImpl"/>

    <context:component-scan base-package="com.bamzhy"/>

    <aop:aspectj-autoproxy />

    <mvc:annotation-driven></mvc:annotation-driven>

</beans>
  • 核心servlet
package com.bamzhy.Controller;


import com.bamzhy.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.jws.WebService;
import java.sql.SQLException;

@Controller
public class CoreController {
    @RequestMapping("/transferto")
    public void test(String user1,String user2,int num,Model model) throws SQLException {
        System.out.println("transfer生效了");
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService service = (UserService) applicationContext.getBean("service");
        service.transfer(user1,user2,num);
    }
}
  • 网页代码
<%--
  Created by IntelliJ IDEA.
  User: Bam
  Date: 2018/4/2
  Time: 21:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title></title>
  </head>
  <body>
  <form action="${pageContext.request.contextPath}/transferto" method="post">
    转账人:<input type="text" name="user1"></br>
    收款人:<input type="text" name="user2"></br>
    金额::<input type="text" name="num"></br>
    <input type="submit" value="转账"></br>
  </form>
  </body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值