springMVC环境搭建(1)
工作一年以来,写的都是.net,最近比较闲,想把之前学过的java相关的东西捡起来,也学点新的东西。以前做过SSH架构,一直好奇spring mvc是怎么样的,所以今天试试看。
总体的代码结构
手动输入地址:http://localhost:8080/SpringMVC01/Login/getLoginPage,运行结果如下
登陆进去后为:
下面是编码过程。
第一步 新建一个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="登陆" /> <input type="button" value="重置" onclick="clearLoginInfo();" /></td> </tr> </table> </form> </body> </html>
js静态文件地址如下。
对应的login.js
/** * 登陆页面 */ // 重置登陆信息 function clearLoginInfo() { $("input[type='text']").val(""); $("input[type='password']").val(""); $("input[type='hidden']").val(""); }
总结
以上东西,都是我百度或者google到的示例写出来的,我看了一下spring的文档,全是英文的,看起来还是有点吃力,感觉还有很多不知道的。不过就上面的东西应该可以解决大部分的问题。不过可能不是最优的。
出处:http://www.cnblogs.com/BestNow/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?