• j2ee之springMVC简单实例


    pom.xml依赖文件

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.newtouch.cn</groupId>
        <artifactId>MySpringMVC</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>MySpringMVC Maven Webapp</name>
        <url>http://maven.apache.org</url>
        <dependencies>
    <!-- springMVC start -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.3.10.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.3.10.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>4.3.10.RELEASE</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.1.1</version>
            </dependency>
    <!-- springMVC end -->
    <!-- jackson start -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.7.4</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.7.4</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.7.4</version>
            </dependency>
    <!-- jackson end -->
    <!-- jstl start -->
         <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
    <!-- jstl end -->
        </dependencies>
        <build>
            <finalName>MySpringMVC</finalName>
        </build>
    </project>

    spring.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
         http://www.springframework.org/schema/cache
         http://www.springframework.org/schema/cache/spring-cache-3.0.xsd">
        <context:component-scan base-package="com.*" />
        <mvc:annotation-driven/>
    </beans>

    web.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring.xml</param-value>
            </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
      <display-name>Archetype Created Web Application</display-name>
    </web-app>

    User.java实体类

    package com.newtouch.action;
    
    import java.util.Date;
    
    public class User {
        private int id;
        private String name;
        private Date birth;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Date getBirth() {
            return birth;
        }
        public void setBirth(Date birth) {
            this.birth = birth;
        }
        
    }

    TestAction.java

    package com.newtouch.action;
    
    import java.util.Date;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    @Controller
    @RequestMapping("/user")
    public class TestAction {
    
        @RequestMapping("/first")
        public @ResponseBody User first() {
            User user = new User();
            user.setId(1);
            user.setName("邵欣");
            user.setBirth(new Date());
            
            return user;
        }
        
        
    }

    index.jsp显示页面

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
        <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
    <html>
    <head>
        <script type="text/javascript" src="js/jquery-3.2.1.js"></script>
    
    </head>
    <body>
    <h2>Hello World!</h2>
    <a href="${pageContext.request.contextPath }/user/first.action">success</a>
    <input type="button" value="成功"/>
    <input type="text" value=""/>
    <script type="text/javascript">
        $(":button").click(function(){
            var url = "${pageContext.request.contextPath }/user/first.action";
            var sendData = null;
            $.post(url , sendData ,function(backData , textStatu , ajax){
                var dateTime = backData.birth;
                var date = new Date(dateTime);
                alert(date.getFullYear()+"年"+(date.getMonth()+1)+"月"+(date.getDate())+"日");
            });
        });
    </script>
    </body>
    </html>
  • 相关阅读:
    dict
    list & tuple
    int & bool & string
    关于gcc内置的原子操作函数
    关于quicklz压缩算法在游戏中的应用
    为mingw生成mysql的客户端库文件
    linux下core生成与调试
    linux下GCC编译动态库切记加 -fPIC
    一则gvim命令
    WIN系统下网络莫名其妙怪异的无法可用时的处理方式
  • 原文地址:https://www.cnblogs.com/ShaoXin/p/7501663.html
Copyright © 2020-2023  润新知