【實例】使用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>
發佈了85 篇原創文章 · 獲贊 18 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章