springMVC学习笔记
一、springMVC环境搭建
1、新建一个文件,选择 java →pack Explore视图
2、把libs下的requai下的jar包引入进来
3、web.xml启动配置springMVC
<servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>【应该是这个jar包下的spring-webmvc-3.2.0.RELEASE.jar下的org.springframework.web.servlet根目录下】
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
4、配置srimgmvc的配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <bean name="/test1/helloworld" class="com.tgb.web.controller.HelloWorldController" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
上面的class="org.springframework.web.servlet.view.InternalResourceViewResolver"
在spring-webmvc-3.2.0.RELEASE.jar下的
org.springframework.web.servlet.view下的
InternalResourceViewResolver.class 视图分解的解析器
二、第二节
1、新建一个controller。
package com.tgb.web.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloWorldController implements Controller{ @Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { System.out.println("helloworld,视频第一个springmvc"); return new ModelAndView("/welcome"); } }
spring如何找到这个controller呢
在spring的配置文件 spring-servlet.xml【默认它的路径和web.xml同级。web.xml→spring-servlet.xml】
could not find the resource 文件spring-servlet.xml【解决方案:spring-servlet.xml修改成springMVC-servlet.xml.修改成默认的文件名了】
填写<bean name="/test1/helloworld" class="com.tgb.web.controller.HelloWorldController" />
最后:浏览器输入http://localhost:8080/springMVC1/test1/helloworld 即可访问
三、修改配置文件和给界面传递参数
1、配置文件不在默认路径下
在src下面新建一个config目录,
查看分发器的源代码。可以知道如何寻找的目录。为contextConfigLocation
修改web.xml springMVC的初始化参数
<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/spring-servlet.xml</param-value> </init-param>
2、传递参数
在control里面 //返回string数和//下面是返回map数据
String hello = "lsh hello 提高班";
Map<String,Object> map = new HashMap<String,Object>();
map.put("map1", "提高班1");
map.put("map2", "提高班2");
map.put("map3", "提高班3");
//返回string数据
//return new ModelAndView("/welcome","result",hello);
//下面是返回map数据
return new ModelAndView("/welcome","map",map);
要用estl表达式,引入下面两个包。
jstl的jar包
standard-1.1.2的jar包
四、springmvc实现一个controller书写多个方法
新建一个controller
1、public class MultiController 继承一个MultiActionController
package com.tgb.web.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class MultiController extends MultiActionController { public ModelAndView add(HttpServletRequest request,HttpServletResponse response){ System.out.println("----add方法ok----"); return new ModelAndView("/multi","method","add"); } public ModelAndView update(HttpServletRequest request,HttpServletResponse response){ System.out.println("-----update---方法"); return new ModelAndView("/multi","method","update"); } }
2、springmvc的配置里面如何写,就是在springMVC-servlet.xml下
<bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="action"></property> </bean>
在spring webmvc架包下的mvc下的multiaction.ParameterMethodNameResolver
value="action">请求的时候参数也要随着这个变化。
请求标记的一个解析器
继续配置
<bean name="/test1/multi" class="com.tgb.web.controller.MultiController"> <property name="methodNameResolver"> <ref bean="paramMethodResolver"/> </property> </bean>
然后浏览器输入:http://localhost:8080/springMVC1/test1/multi?action=add
请求不到,解决方案public ModelAndView add(HttpServletRequest request,HttpServletResponse response)需要括号里面的两个参数
五、第5节静态文件的访问
1、新建一个controller
package com.tgb.web.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class StaticController extends MultiActionController { public ModelAndView img(HttpServletRequest request,HttpServletResponse response){ return new ModelAndView("/staticFile"); } public ModelAndView jquery(HttpServletRequest request,HttpServletResponse response){ return new ModelAndView("/jquery"); } }
2、新建一个jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
<h>图片访问</h>
<div>
<img alt="图片" src="../img/a.png">
</div>
</body>
</html>
<bean name="/test1/img" class="com.tgb.web.controller.StaticController"> <property name="methodNameResolver"> <ref bean="paramMethodResolver"/> </property> </bean>
地址正确,图片拿不到,静态文件取不到
继续在springmvc的配置文件中,写一个mvc 标签。
不要拦截,直接访问
<!-- 静态资源访问 --> <mvc:resources location="/img/" mapping="/img/**"/> <mvc:resources location="/js/" mapping="/js/**"/>
或者这样解决。在web.xml
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern> 修改为*.do 上面那个spring配置文件<bean name="/test1/img" 也修改为test1/img.do 能请求到那个方法。
</servlet-mapping>
3、jquery的请求
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script> script的加载
<script type="text/javascript">
$(document).ready(function(){
});
</script>