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. .NET Framework的属性类对控件的支持功能

     ToolBoxItem 此属性为类特性.属于工具箱属性,可以设置当前控件是否在工具箱中显示,以及所在工具箱项的类型名称等信息.默认生成的控件都显示在工具箱中. 更多设计时属性介绍: 4.3 属性的 ...

  2. ValueError: Variable conv1/weights already exists.

    跑TensorFlow程序的过程中出现了错误,解决之后再次跑时,报如下错误: ValueError: Variable conv1/weights already exists, 原因: 这是因为我在 ...

  3. css 背景图居中

    参考:http://www.php.cn/css-tutorial-411901.html position : 50% ;或position : center;

  4. css 渐变背景

    background: linear-gradient(left,#fa7f6d, #fc5e7f); left: 从左边开始

  5. 如何 在 jQuery 中的 $.each 循环中使用 break 和 continue

    jQuery中each类似于javascript的for循环 但不同于for循环的是在each里面不能使用break结束循环,也不能使用continue来结束本次循环,想要实现类似的功能就只能用ret ...

  6. putty开源的ssh软件工具

    # 登录远程服务器需要ip和端口即可:还是开源工具用起来无忧无虑.无拘无束,这种感觉实在太舒服了,比起xshell开始免费.后来收费好太多太多,不用担心哪天过期了,想干嘛就干嘛. 软件下载地址:htt ...

  7. 中国 SaaS 企业如何突围?这几点是关键!

    进入2019年,关于SaaS的各种言论甚嚣尘上,有人看好:“SaaS市场得大客户者得天下”,也有人唱衰:“SaaS已死,下一个”.在众说纷纭中,中国SaaS企业的成功之路迷雾重重,前期走的较早的头部S ...

  8. mysql视图详解

    什么是视图  视图是从一个或多个表中导出来的表,是一种虚拟存在的表. 视图就像一个窗口,通过这个窗口可以看到系统专门提供的数据. 这样,用户可以不用看到整个数据库中的数据,而之关心对自己有用的数据. ...

  9. Python之通配符--提取文件中的内容并输出

    前言:我的学习进度其实没有那么快的,因为现在是网络工程师实习,只有晚上一点时间和周末有空,所以周一到周天的学习进度很慢,今天之所以突然跳到通配符是因为工作需要,大体讲一下我的工作需求:网络工程师就是写 ...

  10. redis常见的面试题

    redis和memched有什么区别,为什么单线程的redis比多线程的memched效率高 string:ky类型 hash:字典redis的哈希结构可以使你像在数据库中更新一个属性一样只修改某一项 ...