springMVC初识 源码末尾

来源:互联网 发布:班主任网络研修计划 编辑:程序博客网 时间:2024/05/15 23:44

最近对web开发有点小兴趣,公司的移动端开发不是很忙就索性重新学习下web开发吧。

  1. 这里不对spring框架进行任何的理解,因为我也不理解这个玩意,在安卓开发中,我的理解是跟ButterKnifer差不多吧,反正就是简略开发流程了,(理解比较浅显吧)web开发中的xml配置,真的让我蛋疼,花了一天时间才初步理解这些东西。突然觉得Android Studio开发是如此的简单粗暴。配置什么的都去见鬼吧。
  2. 我的开发环境用的是集成版本的ee,所以很多环境不用配置了,这个挺好的,同事给的。省了很多时间,集成springMVC是直接用Maven仓库来集成的,直接在pom.xml里面配置的,后续会贴出源码,配置对应的web.xml就基本可以使用了。
  3. spring的注解目前还不是很了解,用了两三个吧,觉得不用去配置xml真的很爽的
  4. 下面针对写的一个小小的登陆功能做的springmvc进行理解

  • 首先创建Maven Project 其他都默认选项,在src/main/webapp/WEB-INFO/ 中新建jsp文件夹,在jsp下新建两个jsp,一个login.jsp用来做登陆操作,一个success.jsp 用来做登陆成功跳转
  • success.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="login.html" method="post">
    <p>
    用户名:<input name="username" type="text">
    </p>
    <p>
    密&nbsp;&nbsp;码:<input name="upasswd" type="password">
    </p>
    <p>
    <input type="submit">
    </p>
    </form>
    <font color="red">${error}</font>
    </body>
    </html>


    -
    login.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>登陆成功</title></head><body>    <h1>登陆成功了</h1></body></html>

接下里配置springMVC所必要的文件,在src/main/webapp/WEB-INFO/ 中新建springMVC-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:context="http://www.springframework.org/schema/context"    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"    xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsdhttp://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-4.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-4.0.xsd">    //注意这里的字面意思就是去扫描当前路径下的资源,base-package 一定要跟你的包名一致    <context:component-scan base-package="com.spring.demo" /> </beans>

然后配置web.xml

//我这里少了头部的文件,没有添加,因为添加了会报错,可能是版本问题吧<web-app>    <display-name>Archetype Created Web Application</display-name>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener        </listener-class>    </listener>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath*:applicationContext.xml</param-value>    </context-param>    <servlet>        <servlet-name>spring</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet        </servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>/WEB-INF/springMVC-servlet.xml</param-value>        </init-param>    </servlet>    <servlet-mapping>        <servlet-name>spring</servlet-name>        <url-pattern>*.html</url-pattern>    </servlet-mapping></web-app>

重点来了,以上都是配置文件,现在开始编写spring的注解相关信息
在src/main/java/ 新建包名com.spring.demo.control 创建LoginControl

package com.spring.demo.control;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import com.spring.demo.service.LoginService;//这里是控制层,所以使用的是spring 的controller注解 不用继承HttpServlet了,还是不错的@Controllerpublic class LoginControl {    @Resource    LoginService service;    @Resource    HttpServletRequest request;    //这里不用去web.xm里面配置对应的映射文件,很方便有木有    @RequestMapping("index")     public ModelAndView toLoginPage() {        return new ModelAndView("WEB-INF/jsp/login.jsp");    }    @RequestMapping("login")    public ModelAndView doLogin() {        String loginPageUrl = "WEB-INF/jsp/login.jsp";        String successPageUrl = "WEB-INF/jsp/success.jsp";        String uname = request.getParameter("username");        String upasswd = request.getParameter("upasswd");        return service.doLogin(loginPageUrl, successPageUrl, uname, upasswd);    }}

然后就是创建com.spring.demo.Service.LoginService.java

package com.spring.demo.service;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Service;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Servicepublic class LoginService {    public ModelAndView doLogin(String loginPageUrl, String successPageUrl,            String uname, String upasswd) {        // TODO Auto-generated method stub        if (uname == null || "".equals(uname)) {            return new ModelAndView(loginPageUrl, "error", "用户名不能为空");        }        if (upasswd == null || "".equals(upasswd)) {            return new ModelAndView(loginPageUrl, "error", "密码不能为空");        }        if (uname.equals("admin") && upasswd.equals("123")) {            return new ModelAndView(successPageUrl);        }        return new ModelAndView(loginPageUrl, "error", "账号或密码错误");    }}

如上,其实如果我用移动开发的角度来理解,javaWeb开发springmvc框架的话,我的理解是,基于MVC框架而言,spring的作用就是简单化操作,不需要更多配置,甚至于代码的运行效率更加高,而且MVC框架更加明显,jsp是VIEW层、Control层就是Servlet去执行一些主动行为,通过服务层回调给jsp,只是这里我的理解可能有点执拗,我觉得Servlet层就是Model+Control,这些并没有起到解耦作用。后续可能需要更多代码的累积还能清晰理解web端的mvc吧