美文网首页
环境搭建

环境搭建

作者: 聪明的小一休 | 来源:发表于2019-08-13 12:27 被阅读0次

在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.neuedu</groupId>
    <artifactId>TestSpringMVC</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>TestSpringMVC Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <!--指定jdk版本 -->
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>
        <!--文件上传  -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>TestSpringMVC</finalName>
    </build>
</project>

编写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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 配置处理器 -->
    <bean name="/hello.action"
        class="com.neuedu.controller.HelloWorldController"></bean>

    <!-- 配置注解方式的处理器映射器 和 适配器 -->
    <mvc:annotation-driven
        conversion-service="dataConverter"></mvc:annotation-driven>

    <!-- 开启包扫描 -->
    <context:component-scan
        base-package="com.neuedu.controller"></context:component-scan>


    <!-- 配置视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 配置日期转换器 -->
    <bean id="dataConverter"
        class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <list>
                <bean class="com.neuedu.utils.CustomDateConverter"></bean>
            </list>
        </property>
    </bean>

</beans>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>springmvc</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>
  </servlet>
  
    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
    
    </servlet-mapping>
</web-app>

注意: classpath:springmvc.xml用于配置spring mvc的配置文件的位置和名称,这里说明会新建一个springmvc.xml的配置文件。
这里的servlet-mapping表示拦截的模式,这里是“*.action”,表示对于.action结尾的请求进行拦截。

编写控制器

package com.neuedu.controller;

import java.util.List;
import java.util.ArrayList;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.neuedu.test.po.Student;
@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/test3.action")
    public ModelAndView test() {
        ModelAndView mav=new ModelAndView();
        mav.addObject("message","test3");
        List<Student> list=new ArrayList<>();
        Student s1=new Student();
        s1.setStuno(123);
        s1.setStuname("asd");
        Student s2=new Student();
        s2.setStuno(1234);
        s2.setStuname("21d");
        list.add(s1);
        list.add(s2);
        mav.addObject("list",list);
        mav.setViewName("index");
        
        return mav;
    }
    @RequestMapping("/test4.action")
    public ModelAndView test2() {
        ModelAndView mav=new ModelAndView();
        
        mav.addObject("message","test4");
        
        mav.setViewName("index");
        
        return mav;
    }
}

返回String类型

package com.neuedu.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/string")
public class ReturnStringController {

    
        //方法中注入的参数:Model,HttpServletRequest,HttpSession,ServletContext,HttpServletResponse
    
        @RequestMapping("/forword.action")
        public String test1(Model model) {
            //放在model中的内容会加载在request中
            model.addAttribute("message","forword");
            // /WEB-INF/jsp/index.jsp
            return "index";
        }
        
        
        @RequestMapping("/forword2.action")
        public String test2(HttpServletRequest request) {
            //放在model中的内容会加载在request中
            request.setAttribute("message","forword2");
            // /WEB-INF/jsp/index.jsp
            return "index";
        }
        @RequestMapping("/redirect.action")
        public String test3(HttpSession session) {//HttpServletRequest request传不了数据  HttpSession session可以传数据
            //放在model中的内容会加载在request中
            session.setAttribute("message","redirect");
            // /WEB-INF/jsp/index.jsp
            return "redirect:/test.jsp";
        }
}

返回void 型

package com.neuedu.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.neuedu.test.po.Student;

@Controller
@RequestMapping("/void")
public class ReturnVoidController {
    @RequestMapping("/ajax.action")
    public void test(HttpServletResponse response) {
        //1.构建一个json字符串
        
        //List<Student>->[{},{}]
        List<Student> list=new ArrayList<>();
        Student s1=new Student();
        s1.setStuno(123);
        s1.setStuname("asd");
        Student s2=new Student();
        s2.setStuno(1234);
        s2.setStuname("21d");
        list.add(s1);
        list.add(s2);
        String jsonstr=JSON.toJSONString(list);
        //2.通过response把json字符串发给客户端。
        try {
            PrintWriter pw=response.getWriter();
            pw.write(jsonstr);
            pw.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        
    }
}

日期转化器

package com.neuedu.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

public class CustomDateConverter implements Converter<String, Date>{

    @Override
    public Date convert(String arg0) {
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        
        try {
            return sdf.parse(arg0);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}

index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
    this is index.jsp ${message}

    <table>
        <c:forEach items="${list}" var="student">
            <tr>
                <td>${student.stuno}</td>
                <td>${student.stuname}</td>
            </tr>
        </c:forEach>
    </table>

</body>
</html>

笔记
很详细的呦

相关文章

网友评论

      本文标题:环境搭建

      本文链接:https://www.haomeiwen.com/subject/kcxvjctx.html