web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
<servlet-name>spmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>spmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
spmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id = "simpleUrlHandlerMapping" class ="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name ="mappings">
<props>
<prop key ="/login.do">loginControl</prop>
</props>
</property>
</bean>
<bean id ="loginControl" class ="com.asm.spmvclogin"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="com.asm.UserNotFoundException">UserNotFoundException</prop>
</props>
</property>
<property name="defaultErrorView">
<value>error</value>
</property>
</bean>
</beans>
spmvclogin类
package com.asm;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class spmvclogin implements Controller {
private String username;
private String password;
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)throws Exception {
username=(String)request.getParameter("username");
password=(String)request.getParameter("password");
return(new UserManager().login(username, password));
}
}
UserManager类
package com.asm;
import org.springframework.web.servlet.ModelAndView;
public class UserManager {
ModelAndView mav;
public UserManager() {}
public ModelAndView login (String username,String password)throws Exception
{
if ("admin".equals(username)&&"admin".equals(password)){
mav= new ModelAndView("success");
mav.addObject("name", username);
}
else if (!"admin".equals(username)){
throw new UserNotFoundException(username);
}
else {
throw new Exception("密码:"+password+"错误"); }
return mav;
}
}
UserNotFoundException
package com.asm;
public class UserNotFoundException extends RuntimeException {
UserNotFoundException(String msg)
{
super(msg);
}
}
异常抛给springMVC,springMVC把异常对象封装到request里,属性名Exception,属性值Exception对象。然后springMVC又到web工厂里拿异常处理器对象,异常处理器对象在prop里找有没有对应的异常类
网友评论