简单的Maven+SpringMVC
一、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的更多相关文章
- 170328、Maven+SpringMVC+Dubbo 简单的入门demo配置
之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程序猿来说,真是一个好消息.最近有时间了,打算做一个demo把dubbo在本地跑起来先. 先copy一段dubbo ...
- Maven+SpringMVC+Dubbo 简单的入门demo配置
转载自:https://cloud.tencent.com/developer/article/1010636 之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程 ...
- maven+springmvc+dubbo+zookeeper
为什么要用dubbo? 还是让官方来解释吧: http://dubbo.io/User+Guide-zh.htm http://dubbo.io/ 一般 nginx+tomcat ...
- 使用intellij idea搭建MAVEN+springmvc+mybatis框架
原文:使用intellij idea搭建MAVEN+springmvc+mybatis框架 1.首先使用idea创建一个maven项目 2.接着配置pom.xml,以下为我的配置 <projec ...
- Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker)
Spring MVC 环境搭建(maven+SpringMVC+mybatis+Freemarker) 一.准备工作 1.Eclipse Java EE IDE(4.4.1) 2.JDK 3.Tomc ...
- IntelliJ IDEA下Maven SpringMVC+Mybatis入门搭建例子
很久之前写了一篇SSH搭建例子,由于工作原因已经转到SpringMVC+Mybatis,就以之前SSH实现简单登陆的例子,总结看看SpringMVC+Mybatis怎么实现. Spring一开始是轻量 ...
- 1.Maven+SpringMVC+Eclipse软件安装配置,Maven报插件错误,Eclipse总是卡死的解决办法,导入一个maven工程后 一直显示importing maven project
使用Maven+SpringMVC+Eclipse软件安装配置过程中的问题: 1.Eclipse总是卡死的解决办法: 一:内存不足所以会卡死,配置一下eclipse.ini修改这几个值就好了-X ...
- maven+springmvc的配置
1. 首先创建1个mavenweb项目 如果没有的话最好是去官网下载一个最新版本的eclipse 里面什么都有 maven/gradle 啥的 2. 选择路径 没啥影响 就是一个路径 默认就行 ...
- 一步步完成Maven+SpringMVC+SpringFox+Swagger整合示例
本文给出一个整合Maven+SpringMVC+SpringFOX+Swagger的示例,并且一步步给出完成步骤. 本人在做实例时发现 http://blog.csdn.net/zth1002/art ...
随机推荐
- 73th LeetCode Weekly Contest Rotated Digits
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...
- 利用mybatis-generator生成po
import java.io.File; import java.util.ArrayList; import java.util.List; import org.mybatis.generator ...
- onbeforeunload与onunload事件
Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过 window.onunload来指定或者在<body>里指定.区别在于o ...
- Ubuntu14.04修改主机名
1.Ubuntu主机名位于/etc/hostname里,将其修改为自己需要的名称 2.修改/etc/hosts文件,将其中127.0.1.1对应的主机名更改为新的主机名,与/etc/hostname里 ...
- Murano 网址一览
[Murano_Meeting] http://eavesdrop.openstack.org/#Murano_Meeting [Murano_Meeting_Agenda] https://wiki ...
- 存储过程 jdbc
package com.itheima.procedure; import java.sql.CallableStatement; import java.sql.Connection; import ...
- 你一直在用的 Spring Boot Starters 究竟是怎么回事
Spring Boot 对比 Spring MVC 最大的优点就是使用简单,约定大于配置.不会像之前用 Spring MVC 的时候,时不时被 xml 配置文件搞的晕头转向,冷不防还因为 xml 配置 ...
- JS语法字典---网友总结
1.document.write(""); 输出语句2.JS中的注释为//3.传统的HTML文档顺序是:document->html->(head,body)4.一个浏 ...
- CI框架更新与删除
$this->load->database(); // $query=$this->db->get('t_repayments'); // $res ...
- ASP Session的功能的缺陷以及解决方案
转http://www.cnblogs.com/jhy55/p/3376925.html 目前ASP的开发人员都正在使用Session这一强大的功能,但是在他们使用的过程中却发现了ASP Sessio ...