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. Referenced assembly does not have a strong name

    Step 1 : Run visual studio command prompt and go to directory where your DLL located. For Example my ...

  2. wps中,怎么快速查看xls中隐藏的图片

    步骤: 页面布局->选择窗格 即可在右方看到"文档中的对象“

  3. java线程队列

    工作原理 1.线程池刚创建时,里面没有一个线程.任务队列是作为参数传进来的.不过,就算队列里面有任务,线程池也不会马上执行它们. 2.当调用 execute() 方法添加一个任务时,线程池会做如下判断 ...

  4. C开发系列-include

    include 在include目录下有两个文件,分别为main.m, abc.txt main.m内容 #include <stdio.h> int main(){ #include & ...

  5. Ionic 分享功能(微博 微信 QQ)

    1.安装插件 cordova plugin add cordova-plugin-wechat --variable wechatappid=你申请微信appid cordova plugin add ...

  6. 洛谷P3749 [六省联考2017]寿司餐厅

    传送门 题解 这几道都是上周llj讲的题,题解也写得十分好了,所以直接贴了几个链接和代码. //Achen #include<algorithm> #include<iostream ...

  7. JEECMS 系统权限设计

    1.用户校验. 登录校验主要围绕着用户后台登陆的url拦截 a.围绕着用户登录过程中设计到两张用户表 jc_user:存储着用户的基本信息 jo_user:存储着用户登录.注册.更新时间及用户密码信息 ...

  8. Win10操作系统安装—U盘作为启动盘—系统安装到固态硬盘中

    利用U盘作为启动盘安装win10操作系统 1.U盘制作为启动盘,制作工具,我选择的是大白菜(个人觉得还是很好用的) 大白菜http://www.bigbaicai.com/rjjc/syjc/3269 ...

  9. 好用的Win10快捷键

    好用的Win10快捷键 Top 01 基础按键 Win+E: 打开"资源管理器". Win+R: 打开"运行"对话框. Win+L: 锁定当前用户. Win+D ...

  10. 洛谷P3298 泉

    时空限制 1000ms / 128MB 题目描述 作为光荣的济南泉历史研究小组中的一员,铭铭收集了历史上x个不同年份时不同泉区的水流指数,这个指数是一个小于. 2^30的非负整数.第i个年份时六个泉区 ...