码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC环境搭建(1)

时间:2014-12-16 22:32:11      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   ar   io   color   os   sp   

工作一年以来,写的都是.net,最近比较闲,想把之前学过的java相关的东西捡起来,也学点新的东西。以前做过SSH架构,一直好奇spring mvc是怎么样的,所以今天试试看。

总体的代码结构

bubuko.com,布布扣

手动输入地址:http://localhost:8080/SpringMVC01/Login/getLoginPage,运行结果如下

bubuko.com,布布扣

登陆进去后为:

bubuko.com,布布扣

下面是编码过程。

第一步 新建一个Dynamic Web Project,名称为:SpringMVC01

修改WEB-INF下面的web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SpringMVC01</display-name>
  <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
  </context-param>
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener
  </listener-class>
  </listener>
</web-app>

注意:

(1)servlet-name为springmvc下,配置的url-servlet值为:/。这样配置后,需要在springmvc-servelt.xml,进行特殊配置,不然js,css等静态文件都访问不到

(2)注意param-name为contextConfigLocation,配置的param-value值为:springmvc-servlet.xml跟后面新建的文件名要一样,不然tomcat启动会报文件找不到错误

在WEB-INFO下新建一个文件:springmvc-servlet.xml,配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.amy.controller"></context:component-scan>
    <mvc:resources mapping="/js/**" location="/resources/js/"></mvc:resources>
    <mvc:resources mapping="/jslib/**" location="/resources/jslib/"></mvc:resources>
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

注意:

(1)mvc:resources 要进行配置,这样静态的js,css文件才可以访问得到。

(2)prefix配置的值为/WEB-INFO/jsp/。

第二步 写Controller部分

package com.amy.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.catalina.connector.Request;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

    /**
     * 这个请求返回JSP
     * 
     * @param model
     * @return
     */
    @RequestMapping(value = "/Login/getLoginPage", method = RequestMethod.GET)
    public String getLoginPage(ModelMap model) {
        model.addAttribute("message", "Login Spring MVC Framework!");
        return "login/login";
    }

    /**
     * 这个请求返回JSON数据
     * 
     * @return
     */
    @RequestMapping(value = "/user", produces = "application/json")
    @ResponseBody
    public Map<String, Object> GetUserInfoJson() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("uid", "950073");
        map.put("pwd", "123456");
        return map;
    }

    @RequestMapping(value = "/Login/doLogin", method = RequestMethod.POST)
    public String doLogin(String userName, String userPwd, ModelMap model) {
        if (userName.equals("amy") && userPwd.equals("111111")) {
            model.addAttribute("userName", userName);
            return "home/HelloWorld";
        } else {
            model.addAttribute("tips", "您输入的用户名或密码有误");
            return "login/login";
        }
    }
}

以进入登录界面为例子。浏览器输入地址:http://localhost:8080/SpringMVC01/Login/getLoginPage,进入到方法:getLoginPage中,去找地址:login/login。由于springmvc-serlvet.xml中配置了prefix:/WEB-INFO/jsp。因此实际上是访问到

/WEB-INFO/jsp/login/login.jsp。

依赖jar包如下,实际上我不知道要用到哪些,于是我把它们都加进来了。

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
        <attributes>
            <attribute name="owner.project.facets" value="java"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v8.0">
        <attributes>
            <attribute name="owner.project.facets" value="jst.web"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-aop-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-aop-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-aop-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-aspects-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-aspects-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-aspects-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-beans-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-beans-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-beans-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-context-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-context-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-context-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-context-support-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-context-support-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-context-support-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-core-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-core-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-core-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-expression-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-expression-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-expression-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-instrument-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-instrument-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-instrument-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-instrument-tomcat-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-instrument-tomcat-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-instrument-tomcat-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-jdbc-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-jdbc-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-jdbc-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-jms-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-jms-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-jms-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-messaging-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-messaging-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-messaging-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-orm-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-orm-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-orm-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-oxm-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-oxm-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-oxm-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-test-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-test-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-test-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-tx-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-tx-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-tx-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-web-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-web-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-web-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-webmvc-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-webmvc-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-webmvc-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-webmvc-portlet-4.1.2.RELEASE-javadoc.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-webmvc-portlet-4.1.2.RELEASE-sources.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-webmvc-portlet-4.1.2.RELEASE.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-beanutils-1.7.0.jar"/><!-- 这个部分是日志部分,如果不加的话,tomcat启动就会报错 -->
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-collections-3.2.1.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-httpclient-3.1.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-lang-2.3.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-logging-1.1.1.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ezmorph-1.0.3.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/json-lib-2.2.3-jdk15.jar"/>
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jstl.jar"/>  <!-- 这部分是一个标签库,新建一个j2EE1.4的java web项目会自动生成,后面用得到 -->
    <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/standard.jar"/>
    <classpathentry kind="output" path="WebRoot/WEB-INF/classes"/>
</classpath>

第三步 页面部分

Login.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%> <!-- 引入c标签,加载js等静态文件 -->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP ‘login.jsp‘ starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript"
    src="<c:url value=‘/jslib/jquery-1.7.2.js‘/>"></script>
<script type="text/javascript" src="<c:url value=‘/js/login.js‘/>"></script>
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

</head>

<body>
    <h2>${message}</h2>
    <form action="Login/doLogin" method="post">
        <table>
            <tr>
                <th></th>
                <th><input type="hidden" value="12121212" /></th>
            </tr>
            <tr>
                <td></td>
                <td><span style="color:red;">${tips}</span></td>
            </tr>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="userName" value="${userName }" /></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="userPwd" value="${userPwd }" /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="登陆" />&nbsp;&nbsp;<input
                    type="button" value="重置" onclick="clearLoginInfo();" /></td>
            </tr>
        </table>
    </form>
</body>
</html>

js静态文件地址如下。

bubuko.com,布布扣

对应的login.js

/**
 * 登陆页面
 */

// 重置登陆信息
function clearLoginInfo() {
    $("input[type=‘text‘]").val("");
    $("input[type=‘password‘]").val("");
    $("input[type=‘hidden‘]").val("");
}

总结

  以上东西,都是我百度或者google到的示例写出来的,我看了一下spring的文档,全是英文的,看起来还是有点吃力,感觉还有很多不知道的。不过就上面的东西应该可以解决大部分的问题。不过可能不是最优的。

 

springMVC环境搭建(1)

标签:des   style   blog   http   ar   io   color   os   sp   

原文地址:http://www.cnblogs.com/tianxue/p/4168097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!