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>

最新文章

  1. Can&#39;t load IA 32-bit .dll on a AMD 64-bit platform错误的解决
  2. linux命令(1):ls命令
  3. C#Light V0.08A 执行字符串中的C#
  4. 广义表 Head Tail
  5. LR之脚本调试
  6. iOS的WebView中使用javascript调用原生的api
  7. PAT-乙级-1046. 划拳(15)
  8. 解决Navicat Error: Missing required libmysql_d.dll
  9. 如何屏蔽LOGD\LOGI等打印输出
  10. BZOJ 1069 最大土地面积
  11. 用endsWith()来限制图片的后缀名
  12. (转).net控件dropdownlist动态绑定数据
  13. 从零开始学Axure原型设计(进阶篇)
  14. SignalR Self Host+MVC等多端消息推送服务(2)
  15. [Python] uniform() 函数
  16. java-Enumeration,单向队列Queue及双向队列Deque等容器简单使用
  17. 详解Parcel:快速,零配置web应用打包工具。
  18. luogu 1052 过河
  19. redis清除数据/xargs使用
  20. IIS字体文件添加MIME映射

热门文章

  1. &lt;每日一题&gt;题目25:快速排序
  2. sql里面插入语句insert后面的values关键字可省略
  3. Lucene 评分机制一
  4. html常用标签7-多媒体标签
  5. 机器学习中的隐马尔科夫模型(HMM)详解
  6. 【DM642学习笔记三】flash的烧写
  7. Linq之Sum用法新体会
  8. Linux上用户执行命令记录
  9. vue elementui点击表格当前行radio单选选中
  10. Leetcode434.Number of Segments in a String字符串中的单词数