spring MVC demo

spring mvc 的简单配置示例
1、配置web.xml
<servlet>
	<servlet-name>dispatcherContext</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
	<servlet-name>dispatcherContext</servlet-name>
	<url-pattern>*.do</url-pattern>
</servlet-mapping>

2、在web.xml同路径下配置servlet-name一直的servlet-xml文件
dispatcherContext-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>
<!-- 处理器映射器,使用处理器名字作为URL -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<bean name="/hello.do"
  class="com.linkage.justone4.test.SpringMvcAction">
  <property name="name" value="cheng ren yin" />
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
   <property name="prefix" value="/WEB-INF/jsp/"/>
   <property name="suffix" value=".jsp"/>
</bean>
</beans>

3、action实现 AbstractController
public class SpringMvcAction extends AbstractController {
	private String name ;
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("come on ... spring mvc config ....");
		
		return new ModelAndView("MyJsp","name",name);
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

4、MyJsp.jsp 在/WEB-INF/jsp/MyJsp.jsp 目录下,增加jstl的jar包
jsp - <from action="hell.do" ..... 返回 MyJsp.jsp

相关推荐