springmvc01
1,环境搭建:
将 \spring-framework-3.1.1.RELEASE-with-docs\spring-framework-3.1.1.RELEASE\dist 下的所有jar导入,再导入commons-logging-1.1.3.jar 导入 即可
2, 整体架构
3, 第一个springmvc程序
1) web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springmvc</display-name> <!-- 1,配置springmvc --> <servlet> <!-- 这个servlet的name值是hello,必须有一个对应的hello-servlet.xml文件在WEB-INF下 --> <servlet-name>hello</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <!-- 这个url一定要是 / --> <servlet-name>hello</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2) /WEB-INF/hello-servlet.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 使用annotation配置控制器 --> <context:component-scan base-package="com.yangw.springmvc.controller" /> <mvc:annotation-driven/> <!-- 完成视图的对应 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
3)WelcomeController类
package com.yangw.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class WelcomeController{ //@RequestMapping表示 用哪个url对应 @RequestMapping(value={"/","/welcome"}) public String welcome(){ System.out.println("welcome"); return "welcome"; //返回的逻辑视图 } }
4) WEB-INF/jsp/welcome.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> welcome jsp </body> </html>
4, 请求传递参数给controller
package com.yangw.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class WelcomeController{ //@RequestMapping表示 用哪个url对应 @RequestMapping(value={"/","/welcome"}) //请求传递过来的值如果一定存在,即必不可少的话,使用@RequestParam(value="paramname") //如果参数有可能存在,有可能不存在,就不需要注解,直接使用参数 public String welcome(@RequestParam(value="hello") String hello,String world){ System.out.println(hello); System.out.println(world); System.out.println("welcome"); return "welcome"; //返回的逻辑视图 } }
下面三种传递参数的情形,对应不同的结果
5, controller传递参数给view
package com.yangw.springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;
@Controller public class WelcomeController{ //springmvc使用Model来view传值,也可以使用Map替代Model @RequestMapping(value="/hello") public String hello(Model model){ model.addAttribute("hello", "hehe"); //也可以使用model.addAttribute(Object obj);,此时对象的key默认就是对象类型 //这种简化的情形适合 自定义类型,比如一个User对象 model.addAttribute("nihao"); return "welcome"; //返回的逻辑视图 } }
WEB-INF/jsp/welcome.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> <h3>welcome to springmvc</h3><br/> <!-- 在界面上使用${key}获取controller传递过来的值 --> <h3>${hello } --获取的是model.addAttribute("hello", "hehe");设置的值</h3><br/> <h3>${string } --获取的是model.addAttribute("nihao");设置的值</h3> </body> </html>
----------- 赠人玫瑰,手有余香 如果本文对您有所帮助,动动手指扫一扫哟 么么哒 -----------
未经作者 https://www.cnblogs.com/xin1006/ 梦相随1006 同意,不得擅自转载本文,否则后果自负