SpringMVC的配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--方式二:使用Tomcat默认的Servlet解决静态资源无法请求的问题-->
<!-- 静态资源无法加载-->
<!--<mvc:default-servlet-handler/>-->
<!--方式三:使用新的方式 resources标签-->
<!--有图片之后注释了之后刷新之后还有 要ctrl+F5图片才没因为把缓存去掉了-->
<!--<mvc:resources location="img/" mapping="img/**"/>-->
<!--处理器映射器 (没反应)-->
<!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!--<property name="mappings">
<props>
<prop key="hello.do">FirstController</prop>
<prop key="haha.do">FirstController</prop>
</props>
</property>-->
<property name="urlMap">
<map>
<entry key="hello.do">
<value>FirstController</value>
</entry>
<entry key="haha.do" value="FirstController"></entry>
</map>
</property>
</bean>
<!--注册Hanlder 处理器-->
<bean id="FirstController" class="cn.happy.controller.FirstController"/>
<!-- <bean id="/hello.do" class="cn.happy.controller.FirstController"/>-->
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
<servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
web.xml配置
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app> <display-name>Archetype Created Web Application</display-name> <!--核心控制器的配置 DispatcherServlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet-hander.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <!--方式一:使用Tomcat默认的Servlet解决静态资源无法请求的问题--> <!--<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping>--></web-app>实现Controller接口
public class FirstController implements Controller{
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView model=new ModelAndView();
model.addObject("uname","你是小仙女吗? 回答:是呀,哈哈哈哈");
//model.setViewName("/WEB-INF/zxy.jsp");
model.setViewName("zxy");
return model;
}
}
继承AbstractController
public class SecondController extends AbstractController {
protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ModelAndView model=new ModelAndView();
model.addObject("uname","你是小仙女吗? 回答:是呀,哈哈哈哈");
model.setViewName("zxy");
return model;
}
}
配置2 继承的配置文件JSP页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
你是非常棒的,啦啦啦啦啦啦<br/>
${uname}
<img src="${pageContext.request.contextPath}/img/psb.jpg"/>
</body>
</html>
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="hello.do"> <value>SecondController</value> </entry> </map> </property> </bean> <!--注册Hanlder 处理器--> <bean id="SecondController" class="cn.happy.controller.SecondController"> <property name="supportedMethods"> <set> <value>POST</value> <value>GET</value> </set> </property> </bean> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean></beans>