Lib目录

Java目录

HelloController文件代码

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class HelloController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("Hello SpringMVC");
String name = request.getParameter("name");
System.out.println(name);
// 设置参数,回显到页面
// request.setAttribute("msg", "今天下雨了");
// request.getRequestDispatcher("/hello.jsp").forward(request, response);
ModelAndView mv = new ModelAndView();
mv.addObject("msg", "今天下雨了");
mv.setViewName("/hello.jsp");
return mv;
}
}

AnotationController文件代码

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; @Controller
public class AnotationController {
@RequestMapping(value = "/method")
public ModelAndView method(HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("method");
return null;
}
@RequestMapping(value = "/method2")
public ModelAndView method2(HttpServletResponse response)
throws Exception {
System.out.println("method2");
return null;
}
@RequestMapping(value = "/method3")
public ModelAndView method3(HttpServletResponse response, HttpSession session)
throws Exception {
System.out.println("method3");
return null;
}
@RequestMapping(value = "/method4")
public ModelAndView method4(HttpSession session)
throws Exception {
System.out.println("method4");
System.out.println(session);
ModelAndView mv = new ModelAndView();
mv.setViewName("/hello.jsp");
return mv;
}
@RequestMapping(value = "/method5")
public ModelAndView method5(String name)
throws Exception {
System.out.println(name);
ModelAndView mv = new ModelAndView();
mv.setViewName("/hello.jsp");
return mv;
}
}

DataController文件代码

import com.xmg.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date; @Controller
public class DataController {
// 通过原始方式
@RequestMapping("/data1")
public ModelAndView data1(HttpServletResponse response, HttpServletRequest request) {
request.setAttribute("msg","下午出太阳了");
ModelAndView mv = new ModelAndView();
mv.setViewName("hello.jsp");
return null;
}
// 通过通过ModelAndView addObject方式
@RequestMapping("/data2")
public ModelAndView data2() {
System.out.println("data2");
ModelAndView mv = new ModelAndView();
// Map<String, String> map = new HashMap<>();
// map.put("msg","明天不打球");
// map.put("success","true");
// mv.addAllObjects(map);
mv.addObject("后天放假"); //默认的key:类型的全小写
mv.addObject(new Date());
mv.setViewName("hello.jsp");
return null;
}
// 直接返回对象
@RequestMapping("/data3")
@ModelAttribute("msg")
public User data3() {
// 如果没有ModelAndView和response
// 返回结果会找视图解析器 前缀+请求名+后缀
return new User("xx","123");
}
@RequestMapping("/data4")
public String data4() {
System.out.println("data4");
return "show";
}
@RequestMapping("/data5")
public String data5(Model model) {
System.out.println("data5");
model.addAttribute("msg","等会休息一下");
return "show";
}
@RequestMapping("/data6")
public String data6() {
System.out.println("data6");
return "forward:show.jsp";
}
@RequestMapping("/data7")
public String data7() {
System.out.println("data7");
return "redirect:show.jsp";
}
}

ValueController文件代码

import com.xmg.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @Controller
public class ValueController {
// 1.通过最原始的方式来进行传值
@RequestMapping("/value1")
public ModelAndView value1(HttpServletRequest request, HttpServletResponse response) {
System.out.println("value1");
String name = request.getParameter("name");
String password = request.getParameter("password");
User user = new User(name, password);
System.out.println(user);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/value.jsp");
return modelAndView;
}
// 前台和后台传参
@RequestMapping("/value2")
public ModelAndView value2(String name, String password) {
System.out.println("value2");
User user = new User(name, password);
System.out.println(user);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/value.jsp");
return modelAndView;
}
// 前台和后台传参
@RequestMapping("/value3")
public ModelAndView value3(@RequestParam("name11") String name, String password) {
System.out.println("value3");
User user = new User(name, password);
System.out.println(user);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/value.jsp");
return modelAndView;
}
// 对象传参方式
@RequestMapping("/value4")
public ModelAndView value4(User u) {
System.out.println("value4");
System.out.println(u);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/value.jsp");
return modelAndView;
}
// 地址栏传参
@RequestMapping("/value5/{id}")
public ModelAndView value5(@PathVariable("id")Long id) {
System.out.println("value5");
System.out.println(id);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/value.jsp");
return modelAndView;
}
}

FileController文件代码

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID; @Controller
public class FileController {
@RequestMapping("/upload")
public void upload(MultipartFile file) {
FileOutputStream fos = null;
try {
String lastName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
String fileName = UUID.randomUUID().toString();
InputStream fis = file.getInputStream();
fos = new FileOutputStream("D:\\file\\" + fileName);
IOUtils.copy(fis, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@RequestMapping("/downLoad")
@ResponseBody //代表这次请求全部交给response来做处理
public void DownLoad(HttpServletResponse response) {
FileInputStream fis = null;
try {
// 设置下载头
response.setHeader("Content-Disposition", "attachment;filename=" +
new String("三个小明".getBytes("UTF-8"), "iso8859-1") + ".jpg");
File file = new File("D:\\file\\a.jpg");
fis = new FileInputStream(file);
OutputStream os = response.getOutputStream();
IOUtils.copy(fis, os);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

DemoInterceptor文件代码

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DemoInterceptor implements HandlerInterceptor {
// 在调用控制器方法之前
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object o) throws Exception {
System.out.println("DemoInterceptor.preHandle()");
// 如果返回false为拦截这个请求
return false;
}
// 在调用控制器方法之后,视图渲染之前
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object o, ModelAndView modelAndView) throws Exception {
System.out.println("DemoInterceptor.postHandle()");
}
// 视图渲染之后
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object o, Exception e) throws Exception {
System.out.println("DemoInterceptor.afterCompletion()");
}
}

resources目录

application.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:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
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/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- <bean name="/hello" class="com.xmg.HelloController"/>-->
<!-- <bean class="com.xmg.AnotationController"/>-->
<!-- 添加对静态资源的支持 -->
<!-- <mvc:default-servlet-handler />-->
<!-- 开启扫描 -->
<context:component-scan base-package="com.xmg" />
<!-- 添加对SpringMVC的注解支持 -->
<mvc:annotation-driven/>
<!-- 对静态资源的支持 -->
<mvc:default-servlet-handler />
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置上传文件的bean -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<!-- 设置上传文件的最大尺寸为1MB -->
<property name="maxUploadSize" value="#{1024*1024}"/>
</bean>
<!-- 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/show.jsp"/>
<bean class="com.xmg.DemoInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
</beans>

Webapp目录

hello.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%>
<html>
<head>
<title>Title</title>
</head>
<body>
回显的参数
${msg}
${success}
${string}
${date} </body>
</html>

Show.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>

Value.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<fieldset>
<legend>用户注册-原始方式</legend>
<form action="/value1" method="post" >
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</fieldset>
<fieldset>
<legend>用户注册-原始方式-参数名对应前台名称</legend>
<form action="/value2" method="post" >
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</fieldset>
<fieldset>
<legend>用户注册-原始方式-参数名对应前台名称</legend>
<form action="/value3" method="post" >
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="name11"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</fieldset>
<fieldset>
<legend>用户注册-对象传参</legend>
<form action="/value4" method="post" >
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</fieldset>
</body>
</html>

Upload.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<body>
<fieldset>
<legend>文件上传</legend>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="提交">
</form>
</fieldset>
</body>
</html>

SpringMVC代码复制版的更多相关文章

  1. 编写手机端自适应页面案例,springMVC代码,SpringMVC上传代码,去掉input框中原有的样式,使ios按钮没有圆角,css中的border-radius类似

    1.编写的页面 <%@ page language="java" contentType="text/html; charset=UTF-8"  page ...

  2. Docker — 从入门到实践PDF下载(可复制版)

    0.9-rc2(2017-12-09)修订说明:本书内容将基于DockerCEv17.MM进行重新修订,计划2017年底发布0.9.0版本.旧版本(Docker1.13-)内容,请阅读docker-l ...

  3. ng2-timesheet, 一个timesheet.js的angular2复制版

    一个 timesheet.js (JavaScript library for HTML5 & CSS3 time sheets) 的 Angular 2 复制版 用法: npm instal ...

  4. [C++]数组与指针(纯代码-复习用)

    #include<iostream> #include<cmath> //C++ //#include<math.h> //C #include<ctime& ...

  5. DelphiXE5 Flappy Bird 复制版

    没错 这就是用DelphiXe5 打造的.最流行的 Flappy bird!呵呵. 转 Delphi XE5 Firemonkey Flappy Bird Clone  from fmxexpress

  6. Android中常用的颜色

    代码: <?xml version=”″ ?> <resources> <color name=”white”>#ffffff</color><! ...

  7. PHP代码重用与函数编写

    代码重用与函数编写 1.使用require()和include()函数 这两个函数的作用是将一个文件爱你载入到PHP脚本中,这样就可以直接调用这个文件中的方法.require()和include()几 ...

  8. Java如何搭建脚手架(自动生成通用代码),创建自定义的archetype(项目模板)

    .personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...

  9. 如何在博客中使用SublimeText风格的代码高亮样式

    因为觉得博客园自带的代码高亮样式很单一,不符合作为前端的我的审美习惯,于是下定决心要想办法折腾出一个方法来应用上另外一套代码高亮样式. 虽然探索的过程是很痛苦的,但最后还是成功了,但也不枉付出的那些努 ...

随机推荐

  1. zip压缩工具类

    java将有关zip压缩的内容都封装在java.util.zip宝中,用java实现zip压缩,不用考虑压缩算法,java已经将这些进行了封装 实际上用java实现zip压缩涉及的就是一个“输入输出流 ...

  2. mybatis配合pagehelper分页助手查询

    Maven: 参考: springBoot2.x整合pagehelper5.1.2:https://blog.csdn.net/Carlson_Chis/article/details/8563748 ...

  3. C语言作用域、链接属性和存储类型

    C/C++中作用域详解 作用域 编译器可以确认的4种作用域-代码块作用域.文件作用域.函数作用域和原型作用域,一般来说,标识符(包括变量名和函数名)声明的位置决定它的作用域. (1)代码块作用域 一对 ...

  4. 转:linux select 多路复用机制

    源地址:http://blog.csdn.net/turkeyzhou/article/details/8609360 2013-02-25 14:18 442人阅读 评论(1) 收藏 举报   目录 ...

  5. PAT甲级——A1064 Complete Binary Search Tree

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  6. 023-linux(2)

    1. head 查看文件的前N行 -n ,表示查看前几行 head - test.txt 2. tail 查看文件的后N行 -n,表示查看文件的后几行 tail - test.txt -f(循环读取) ...

  7. jmeter设置代理

    JMeter设置Http代理对web或者app进行录制 一.录制web 1.首先保证JMeter的安装环境都正确.启动JMeter:在安装路径的bin目录下双击jmeter.bat (例如:D:\ap ...

  8. jQuery 取值、赋值的基本方法整理

    /*获得TEXT.AREATEXT的值*/ var textval = $("#text_id").attr("value"); //或者 var textva ...

  9. webServices学习一(了解基础和作用。)

    一.第一部分 1.         带着几个问题学习: l    什么是WebService? l    它能做什么? l    为什么要学习WebService? l    学习WebService ...

  10. Luogu P1963 [NOI2009]变换序列(二分图匹配)

    P1963 [NOI2009]变换序列 题意 题目描述 对于\(N\)个整数\(0,1, \cdots ,N-1\),一个变换序列\(T\)可以将\(i\)变成\(T_i\),其中\(T_i \in ...