博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring-MVC
阅读量:6302 次
发布时间:2019-06-22

本文共 9269 字,大约阅读时间需要 30 分钟。

 

结构:

pom.xml:

4.0.0
com.spring
HelloSpringMVC
war
0.0.1-SNAPSHOT
HelloSpringMVC Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
javax.servlet
javax.servlet-api
3.1.0
provided
org.springframework
spring-core
4.1.4.RELEASE
org.springframework
spring-web
4.1.4.RELEASE
org.springframework
spring-webmvc
4.1.4.RELEASE
HelloSpringMVC
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2

xsi全名:xml schema instance

web-app是web.xml的根节点标签名称
version是版本的意思
xmlns是web.xml文件用到的命名空间
xmlns:xsi是指web.xml遵守xml规范
xsi:schemaLocation是指具体用到的schema资源 

参考:http://www.cnblogs.com/zhao1949/p/5652167.html

 

web.xml:

HelloWorldSpring
spring-mvc
org.springframework.web.servlet.DispatcherServlet
1
spring-mvc
/
contextConfigLocation
/WEB-INF/root-context.xml
org.springframework.web.context.ContextLoaderListener

DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据相应的规则分发到目标Controller来处理,是配置spring MVC的第一步

 

spring-mvc-servlet.xml:

//视图名称解析器
/WEB-INF/pages/
    
.jsp
  

 

root-context.xml:

HelloWorldController.java:

package com.spring.mvc;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping; @Controller //负责注册一个bean 到spring 上下文中public class HelloWorldController {     @RequestMapping("/hello")//注解为控制器指定可以处理哪些 URL 请求    public String hello(Model model) {                 model.addAttribute("hello", "哈喽 Spring MVC!");                 return"helloworld";             } }

index.jsp:

Hello World!

运行应用程序之前,需要构建整个项目。
右键单击该项目并选择:
  • Run As/Maven install

运行配置:

  •  Run HelloSpringMVC
  • ${workspace_loc:/HelloSpringMVC}
  • tomcat7:run

然后运行

访问http://localhost:8080/HelloSpringMVC/

 

 

访问http://localhost:8080/HelloSpringMVC/hello

 --------------------------------完毕-------------------------------

 修改HelloWorldController.java

package com.spring.mvc;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping; @Controllerpublic class HelloWorldController {     @RequestMapping("/hello")    public String hello(Model model) {                 model.addAttribute("hello", "哈喽 Spring MVC!");                 return"helloworld";             }    //在方法中使用 HttpServletRequest, HttpServletResponse     @RequestMapping("/requestResponse")    public String requestResponseExample(HttpServletRequest request,            HttpServletResponse reponses, Model model) {                model.addAttribute("hello", "哈喽 requestResponse");        return "helloworld";    } }

访问http://localhost:8080/HelloSpringMVC/requestResponse

 --------------------------------完毕-------------------------------

重定向到另一页面

修改HelloWorldController.java

package com.spring.mvc;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod; @Controllerpublic class HelloWorldController {     @RequestMapping("/hello")    public String hello(Model model) {                 model.addAttribute("hello", "/hi被redirect到了/hello");                 return"helloworld";             }        @RequestMapping("/requestResponse")    public String requestResponseExample(HttpServletRequest request,            HttpServletResponse reponses, Model model) {                model.addAttribute("hello", "哈喽 requestResponse");        return "helloworld";    }        @RequestMapping(value = "/hi", method = RequestMethod.GET)    public String hi(Model model) {                return "redirect:/hello";    } }

访问http://localhost:8080/HelloSpringMVC/hi

地址会变成http://localhost:8080/HelloSpringMVC/hello

 --------------------------------完毕-------------------------------

新增RequestParamExampleController.java

package com.spring.mvc;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam; @Controllerpublic class RequestParamExampleController {         @RequestMapping("/user")    public String userInfo(Model model,            @RequestParam(value = "name", defaultValue = "Guest") String name) {         model.addAttribute("name", name);         if("admin".equals(name)) {            model.addAttribute("age", "18");        } else{            model.addAttribute("age", "0");        }        return "userInfo";    }  }

新增userInfo.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%>
Insert title here

${name}

Age: ${age}

访问http://localhost:8080/HelloSpringMVC/user:

访问http://localhost:8080/HelloSpringMVC/user?name=admin

 --------------------------------完毕-------------------------------

 添加PathVariableExampleController.java

package com.spring.mvc;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping; @Controllerpublic class PathVariableExampleController {       /**     * @PathVariable Example:     *     */    @RequestMapping("/web/fe/{sitePrefix}/{language}/document/{id}/{naturalText}")//URI模板
public String documentView(Model model, @PathVariable(value = "sitePrefix") String sitePrefix, @PathVariable(value = "language") String language, @PathVariable(value = "id") Long id, @PathVariable(value = "naturalText") String naturalText) { model.addAttribute("sitePrefix", sitePrefix); model.addAttribute("language", language); model.addAttribute("id", id); model.addAttribute("naturalText", naturalText); String documentName = "love"; if(id == 888) { documentName = "love888"; } model.addAttribute("documentName", documentName); return "documentView"; } }

添加documentView.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title here

${documentName}

Site Prefix: ${sitePrefix}
Language: ${language}
ID: ${id}
Natural Text: ${naturalText}

访问http://localhost:8080/HelloSpringMVC/web/fe/love/you/document/000/spring-mvc

访问http://localhost:8080/HelloSpringMVC/web/fe/love/you/document/888/spring-mvc

 --------------------------------完毕-------------------------------

 使用@ResponseBody和方法返回字符串

新增ResponseBodyExample1Controller.java

package com.spring.mvc;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody; @Controllerpublic class ResponseBodyExample1Controller {     // Simple example, method returns String.    @RequestMapping(value = "/ResponseBody")    @ResponseBody    public String authorInfo(Model model) {        return "I love you";    }      }

访问http://localhost:8080/HelloSpringMVC/ResponseBody

此处不需要“.jsp”文件即可显示

 --------------------------------完毕-------------------------------

 参考:http://www.cnblogs.com/sunniest/p/4555801.html

转载于:https://www.cnblogs.com/Alwaysbecoding/p/6945566.html

你可能感兴趣的文章
Factorialize a Number
查看>>
[USB-Blaster] Error (209040): Can't access JTAG chain
查看>>
TreeSet的用法
查看>>
防HTTP慢速攻击的nginx安全配置
查看>>
深入理解PHP内核(十四)类的成员变量及方法
查看>>
Spring Boot2.0+中,自定义配置类扩展springMVC的功能
查看>>
参与博客编辑器改版,我的礼物 感谢51cto
查看>>
JavaWeb笔记——JSTL标签
查看>>
Eclipse插件大全 挑选最牛的TOP30
查看>>
一些实用性的总结与纠正
查看>>
Kubernetes概念
查看>>
逻辑卷管理器(LVM)
查看>>
一个小代码,欢迎大佬的意见,求指正
查看>>
搭建LAMP架构
查看>>
神经网络注意力机制--Attention in Neural Networks
查看>>
Spring.Net+WCF实现分布式事务
查看>>
在Linux上高效开发的7个建议
查看>>
java数据结构 - 数组使用的代码
查看>>
个人简历-项目经验
查看>>
swoole异步任务task处理慢请求简单实例
查看>>