<pre code_snippet_id="2534904" snippet_file_name="blog_20170815_1_6905900" name="code" class="java"><h1><a name="t0"></a>SpringMVC的配置文件</h1>
<?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">
<strong> <!--方式二:使用Tomcat默认的Servlet解决静态资源无法请求的问题--></strong>
<!-- 静态资源无法加载-->
<!--<mvc:default-servlet-handler/>-->
<strong><!--方式三:使用新的方式 resources标签--></strong>
<!--有图片之后注释了之后刷新之后还有 要ctrl+F5图片才没因为把缓存去掉了-->
<!--<mvc:resources location="img/" mapping="img/**"/>-->
<!--<strong>处理器映射器</strong> (没反应)-->
<!-- <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>
<strong> <!--注册Hanlder 处理器--></strong>
<bean id="FirstController" class="cn.happy.controller.FirstController"/>
<!-- <bean id="/hello.do" class="cn.happy.controller.FirstController"/>-->
<strong>
<!--视图解析器--></strong>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
</pre><br>
<servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
<h1><a name="t1"></a>web.xml配置</h1>
<!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>
<strong><!--核心控制器的配置 DispatcherServlet--></strong> <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> <strong>
<!--方式一:使用Tomcat默认的Servlet解决静态资源无法请求的问题--></strong> <!--<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping>--></web-app>
<pre></pre>
<pre code_snippet_id="2534904" snippet_file_name="blog_20170815_2_3001422" name="code" class="java"><pre code_snippet_id="2534904" snippet_file_name="blog_20170815_2_3001422" name="code" class="java"><h1><a name="t2"></a>实现Controller接口</h1>
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;
}
}</pre><br>
<br>
<pre></pre>
<p><br>
</p>
<p></p><pre code_snippet_id="2534904" snippet_file_name="blog_20170815_3_4811566" name="code" class="java"><h1><a name="t3"></a>继承AbstractController</h1>
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;
}
}</pre><pre code_snippet_id="2534904" snippet_file_name="blog_20170815_4_8781300" name="code" class="java"><strong>配置2 继承的配置文件</strong><pre code_snippet_id="2534904" snippet_file_name="blog_20170815_4_8781300" name="code" class="java">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>
</pre><br>
<?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><p></p>
<pre></pre>
<br>
<p></p>
<p><br>
</p>
<br>
<br>
</pre></pre>
SpringMVC
最新推荐文章于 2023-04-25 19:37:05 发布