一、SpringMVC非注解编程

1:修改pom.xml文件(相当于非Maven项目的导入jar包)

   <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>

2:修改配置文件web.xml为以下代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></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-config.xml</param-value> </init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

3:在src/main/resources下面创建springmvc-config.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean name="/hello" class="com.gx.controller.HelloController"/> </beans>

4:在src/main/java里面创建com.gx.controller.HelloController

package com.gx.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller; public class HelloController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response)throws Exception{
System.out.println("handleRequest is working ......");
String myGetMess = request.getParameter("mess");
System.out.println("what I get is "+myGetMess);
ModelAndView mv = new ModelAndView();
mv.addObject("message" , "hello , this is my springmvc");
mv.setViewName("/index.jsp");
return mv;
}
}

5:在webapp下面创建跳转页面index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${requestScope.message}
</body>
</html>

6:在页面输入网址

http://localhost:8888/testSystem/hello?mess="123"

7:页面信息

8:控制台信息

二、SpringMVC注解编程

1:修改pom.xml文件(相当于非Maven项目的导入jar包)

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>

2:修改配置文件web.xml为以下代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></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-config.xml</param-value> </init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

3:在src/main/resources下面创建springmvc-config.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!--
非注解编程的配置文件
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean name="/hello" class="com.gx.controller.HelloController"/>
--> <!-- spring会自动扫描 base-package 下面的包或者java文件 -->
<context:component-scan base-package="com.gx.controller" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" /> </beans>

4:在src/main/java里面创建com.gx.controller.HelloController

package com.gx.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Controller; //public class HelloController implements Controller{
@Controller
public class HelloController{
//public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response)throws Exception{
@RequestMapping(value="/hello")
public ModelAndView useNote(){
System.out.println("method useNote in controller HelloController is working ......");
ModelAndView mv = new ModelAndView();
mv.addObject("message" , "hello , this is my springmvc");
mv.setViewName("/index.jsp");
return mv;
}
}

5:在webapp下面创建跳转页面index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${requestScope.message}
</body>
</html>

6:在页面输入网址

http://localhost:8888/testSystem/hello

7:页面信息

8:控制台信息

三、注册编程获取页面值并跳转

(3.1)ModelAndView

1:在“二”的基础上添加com.gx.model.User

package com.gx.model;

public class User {
String username;
String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
} }

2:创建com.gx.controller.Controller1

package com.gx.controller;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import com.gx.model.User; import org.springframework.stereotype.Controller; @Controller
public class Controller1{
@ModelAttribute
public void useModelAndView(String name,String password,ModelAndView mv){
System.out.println("useModelAndView in Controller1 is working......");
User user = new User();
user.setPassword(password);
user.setUsername(name);
mv.addObject("user",user);
}
@RequestMapping(value="/login1")
public ModelAndView login(ModelAndView mv){
System.out.println("login in Controller1 is working......");
User user = (User)mv.getModel().get("user");
System.out.println(user);
//跳转到welcome.jsp
mv.setViewName("/welcomePage.jsp");
return mv;
}
}

3:创建index.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login1" method="post">
请输入用户名
<input type="text" name="name" />
请输入密码
<input type="password" name="password" />
<input type="submit" value="登录" />
</form>
</body>
</html>

4:创建welcomPage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${requestScope.user.username},欢迎!!
</body>
</html>

5:在页面输入网址

6:点击登录之后的页面信息

7:控制台信息

(3.2)ModelMap

1:在(3.1)的基础上创建com.gx.controller.Controller2

package com.gx.controller;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping; import com.gx.model.User; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap; @Controller
public class Controller2{
@ModelAttribute
public void useModelMap(String name,String password,ModelMap modelMap){
System.out.println("useModelMap in Controller2 is working......");
User user = new User();
user.setPassword(password);
user.setUsername(name);
modelMap.addAttribute("user",user);
}
@RequestMapping(value="/login2")
public String login(ModelMap modelMap){
System.out.println("login in Controller2 is working......");
/*
* 在Controller1里面
* User user = (User)mv.getModel().get("user");
*/
User user = (User)modelMap.get("user");
System.out.println(user);
/*
* 在Controller1里面跳转到welcome.jsp
* mv.setViewName("/welcomePage.jsp");
* return mv;
*/
//跳转到welcome.jsp
return "welcomePage.jsp";
}
}

2:修改index.jsp里面form表单action为login2

3:在页面输入网址

4:点击确定之后页面信息

5:控制台信息

(3.3)Model

1:在(3.1)的基础上创建com.gx.controller.Controller3

package com.gx.controller;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping; import com.gx.model.User; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; @Controller
public class Controller3{
@ModelAttribute
public void useModel(String name,String password,Model model){
System.out.println("useModel in Controller3 is working......");
User user = new User();
user.setPassword(password);
user.setUsername(name);
model.addAttribute("user",user);
}
@RequestMapping(value="/login3")
public String login(Model model){
System.out.println("login in Controller3 is working......");
/*
* 在UseModelController1里面
* User user = (User)mv.getModel().get("user");
* 在UseModelController2里面
* User user = (User)modelMap.get("user");
*/
User user = (User)model.asMap().get("user");
System.out.println(user);
//跳转到welcome.jsp
return "welcomePage.jsp";
}
}

2:修改index.jsp里面form表单action为login3

3:在页面输入网址

4:网页信息

5:控制台信息

(3.4)@RequestParam

1:在(3.1)的基础上创建com.gx.controller.Controller4

package com.gx.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import com.gx.model.User; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; @Controller
public class Controller4{
@RequestMapping(value="/login4")
public String login(@RequestParam("name")String username,
@RequestParam("password")String password,
Model model){
System.out.println("login in Controller4 is working......");
User user = new User();
user.setPassword(password);
user.setUsername(username);
System.out.println(user);
model.addAttribute("user",user);
//跳转到welcome.jsp
return "welcomePage.jsp";
}
}

2:修改index.jsp里面form表单action为login4

3:在页面输入网址

4:页面信息

5:控制台信息

简单的Maven+SpringMVC的更多相关文章

  1. 170328、Maven+SpringMVC+Dubbo 简单的入门demo配置

    之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程序猿来说,真是一个好消息.最近有时间了,打算做一个demo把dubbo在本地跑起来先. 先copy一段dubbo ...

  2. Maven+SpringMVC+Dubbo 简单的入门demo配置

    转载自:https://cloud.tencent.com/developer/article/1010636 之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程 ...

  3. maven+springmvc+dubbo+zookeeper

        为什么要用dubbo?   还是让官方来解释吧: http://dubbo.io/User+Guide-zh.htm   http://dubbo.io/   一般 nginx+tomcat ...

  4. 使用intellij idea搭建MAVEN+springmvc+mybatis框架

    原文:使用intellij idea搭建MAVEN+springmvc+mybatis框架 1.首先使用idea创建一个maven项目 2.接着配置pom.xml,以下为我的配置 <projec ...

  5. Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker)

    Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker) 一.准备工作 1.Eclipse Java EE IDE(4.4.1) 2.JDK 3.Tomc ...

  6. IntelliJ IDEA下Maven SpringMVC+Mybatis入门搭建例子

    很久之前写了一篇SSH搭建例子,由于工作原因已经转到SpringMVC+Mybatis,就以之前SSH实现简单登陆的例子,总结看看SpringMVC+Mybatis怎么实现. Spring一开始是轻量 ...

  7. 1.Maven+SpringMVC+Eclipse软件安装配置,Maven报插件错误,Eclipse总是卡死的解决办法,导入一个maven工程后 一直显示importing maven project

     使用Maven+SpringMVC+Eclipse软件安装配置过程中的问题: 1.Eclipse总是卡死的解决办法: 一:内存不足所以会卡死,配置一下eclipse.ini修改这几个值就好了-X ...

  8. maven+springmvc的配置

    1. 首先创建1个mavenweb项目  如果没有的话最好是去官网下载一个最新版本的eclipse  里面什么都有 maven/gradle 啥的 2. 选择路径   没啥影响 就是一个路径 默认就行 ...

  9. 一步步完成Maven+SpringMVC+SpringFox+Swagger整合示例

    本文给出一个整合Maven+SpringMVC+SpringFOX+Swagger的示例,并且一步步给出完成步骤. 本人在做实例时发现 http://blog.csdn.net/zth1002/art ...

随机推荐

  1. 1093 Count PAT's(25 分)

    The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and ...

  2. Chapter13

    package scala /** * Created by EX-CHENZECHAO001 on 2018-04-04. */class Chapter13 { // 13 集合 // 所有的集合 ...

  3. Linux重新挂载磁盘

    Linux下磁盘和目录的概念与WIN不同:比如,分了一个系统分区默认挂载了根(/)目录,根下还有其它目录,比如/user /lib等.如果系统分区不够用,可以再分出分支,把根下其它目录分别挂载出来,例 ...

  4. Python——连接数据库

    好用的教程(*^▽^*):https://www.cnblogs.com/fatcat132006/p/4081576.html

  5. nodejs加密解密

    nodejs是通集成在内核中的crypto模块来完成加密解密. 常用加密解密模块化代码: /** * Created by linli on 2015/8/25. */ var crypto = re ...

  6. PHP session变量的销毁

    1.何为session? 相当于一个客户端(可以是浏览器.app.ftp等其他,而且同一个浏览器多开几个又算是不同的客户端)对服务器的一个访问,这个期间服务器为此建立一个唯一的标示(session_i ...

  7. SpringBoot | 第十六章:web应用开发

    前言 前面讲了这么多直接,都没有涉及到前端web和后端交互的部分.因为作者所在公司是采用前后端分离方式进行web项目开发了.所以都是后端提供api接口,前端根据api文档或者服务自行调用的.后台也有读 ...

  8. Entity framework 7通过代码添加外键关系的方法

    这几天研究Asp.net5,也试着写了一些示例代码,因为网上的资料实在是太少了,所以在此把一些问题的解决方法记录下来,以备后查. 问题: 在EF7中,假如数据库已经存在,并且两个表具有外键关系,但是实 ...

  9. 初识MAC(由window到mac的转变适应)

    * Windows上的软件可以拿到Mac上面安装吗? Windows上面的软件不能拿到Mac的操作系统上安装,除此之外,Windows里的 exe文件,在Mac下面也是无法运行的,要特別注意.如果你要 ...

  10. python之其他模块的用法

    1.时间模块   在Python中通常有三种表示时间的方式,分别是时间戳.元组.格式化的时间字符串. 时间模块的常用方法 time.sleep() #指定延迟时间 time.time() #当前时间的 ...