欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring SpringWebJavaJSPServlet 

程序员文章站 2022-06-17 09:08:31
...
今天看spring的几篇资料,发现ioc和dependent injection居然指的一个东西,以前还只知道ioc,现在是焕然大悟。
spring web framework
这个讲的 spring的mvc机制,流程。
有client--->web.xml-----DispatchServlet-->controllers--->BaseLogic Layer---->controllers------->DispatchServlet--->client.
下面基本的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<servlet>
<servlet-name>users</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>users</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/spring</taglib-uri>
<taglib-location>/WEB-INF/spring.tld</taglib-location>
</taglib>
</web-app>

通过servlet.xml映射页面或是controller.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/springbeans.
dtd">
<beans>
<!— Mapping definitions: map urls to action ids -->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/userhome.htm">userHomeAction</prop>
</props>
</property>
</bean>
<!-- Action definitions -->
<bean id="userHomeAction" class="org.springwebtest.UserHomeController">
<property name="viewFilename"><value>/WEB-INF/jsp/hello.jsp</value></property>
</bean>
<!-- This is to define jstl view -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
</bean>
</beans>

错误处理:在servlet.xml里加入
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.sql.SQLException">caughtexception</prop>
<prop key="java.lang.RuntimeException">caughtexception</prop>
</props>
</property>
</bean>

caughtexception 页面是这样的:
<html>
<head><title>System failure</title></head>
<body>
<% Exception ex = (Exception);request.getAttribute(“Exception”);; %>
<H2>System failure cause: <% ex.getMessage(); %></H2>
<P/>
<% ex.printStackTrace(new java.io.PrintWriter(out);; %>
</body>
</html>

而无法预计的异常uncaughtexception 需在web.xml里面加入
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/WEB-INF/jsp/uncaughtexception.jsp</location>
</error-page>

它的页面就是uncaughtexception.jsp,输入异常。
而在servlet.xml里的forward -redirect示例如下。
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/userhome.htm">userHomeAction</prop>
<prop key="/input1show.htm">input1ShowAction</prop>
<prop key="/input1post.htm">input1PostAction</prop>
[color=red]<prop key="/redirect.htm">redirectHomeAction</prop>[/color]
</props>
</property>
</bean>
<!-- redirect action -->[color=red]
<bean id="redirectHomeAction" class="org.springwebtest.URLRedirectController">[/color]
<property name="viewFilename"><value>/spring/userhome.htm</value></property>
</bean>