01.SpringMVC基础【孔浩SpringMVC视频教程】学习笔记

来源:互联网 发布:天蝎网络第三季百度云 编辑:程序博客网 时间:2024/04/26 13:44

学习springMVC步骤:1,搭建环境2,如何完成Controller和viewer的映射3,如何把值传给Controller4,Controller如何把值传给viewer5,异常处理6,页面标签7,文件上传8,深入一下源代码搭建环境:(1)创建web项目,导入相应的包,spring中的包,Apache中commons-login包。(2)配置DispatcherServlet,在web.xml中添加:<servlet>       <servlet-name>hello</servlet-name>       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>       <load-on-startup>1</load-on-startup></servlet><servlet-mapping>       <servlet-name>hello</servlet-name>       <url-pattern>/</url-pattern></servlet-mapping>

创建一个hello-servlet.xml来管理spring的一些配置:

可以在Web MVC framework 中找:

<?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:p="http://www.springframework.org/schema/p"     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/context         http://www.springframework.org/schema/context/spring-context-3.0.xsd">    //<context:component-scan base-package="org.springframework.samples.petclinic.web"/>        <bean name="welcome.html" class="zttic.itat.controller.WelcomeController"></bean>    <bean class="org.springframework.web.servlet.view.InternalResuorceViewResolver"        <property name="prefix" value="/WEB-INF/jsp/"/>        <property name= "surfix", value=".jsp"/>     </bean></beans>

建立一个WelcomeController.java:

package zttc.itat.controller;import javax.servlet.http.HttpServletRequest;public class WelcomeController extends AbstractController{    @Override    protected ModelAndView handleRequestInternal(httpServletRequest request,                   httpServletResponse response) throws Exception{            System.out.println("welcome");            return new ModelAndView("welcome");    }}

建立一个welcome.jsp文件,在jsp文件夹中(新建一个在WEB-INF目录下):

<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title></title></head><body><h1>welcome!!</h1></body>

浏览器中输入localhost:8080/springmvc_hello/welcom.jsp 可以得到值

但是上面这种方式没法增删改查,所以一般很少使用。

实际上,在开发中,我们一般使用annotation

在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:p="http://www.springframework.org/schema/p"     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/context         http://www.springframework.org/schema/context/spring-context-3.0.xsd"> //<context:component-scan base-package="org.springframework.samples.petclinic.web"/>    //这两句是打开annotation    <context:component-scan base-package="zttc.itat.controller">    <mvc:annotation-driven/>    //<bean name="welcome.html" class="zttic.itat.controller.WelcomeController"></bean>    <bean class="org.springframework.web.servlet.view.InternalResuorceViewResolver"        <property name="prefix" value="/WEB-INF/jsp/"/>        <property name= "surfix", value=".jsp"/>     </bean></beans>

创建控制器,新建HelloController.java

package zttc.itat.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloController{    //RequestMapping表示用哪个URL来对应    @RequestMapping{{"/hello","/"}}    public String hello(){           System.out.println("hello");           return "hello";     }      @RequestMapping{{"/welcome"}}     public String welcome(){           System.out.println("welcome");           return "welcome";      } }

在jsp目录下创建hello.jsp

运行,输入localhost:8080/springmvc_hello/hello就可以看到页面。



2 0
原创粉丝点击