spring MVC
和struts2 一样 使用springMVC 也要在web.xml配置好过滤器 ,但是这里用的不是过滤器 ,而是Servlet , 配置如下:dispatcherorg.springframework.web.servlet.DispatcherServletcontextConfigLocation
·
和struts2 一样 使用springMVC 也要在web.xml配置好过滤器 ,但是这里用的不是过滤器 ,而是Servlet , 配置如下:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value> <!-- 制定springMVC核心控制文件路径,就像struts.xml一样 -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
接下来就配置 dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-autowire="byName">
<!-- SpringMVC相关Bean配置 -->
<!-- View Resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" /> <!-- 指定Control返回的View所在的路径 -->
<property name="suffix" value=".jsp" /> <!-- 指定Control返回的ViewName默认文件类型 -->
</bean>
<bean id = "simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/userLogin.do">UserLoginControl</prop> <!-- 配置Contorl ,UserLoginControl使用Spring的IOC注入 -->
</props>
</property>
</bean>
</beans>
再接下来就开始写Control
package com.zf.control;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.zf.dao.PersonDao;
import com.zf.pojo.Person;
@org.springframework.stereotype.Controller("UserLoginControl")
public class UserLoginControl implements Controller{
@Resource(name = "PersonDao")
private PersonDao personDao;
@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
ModelAndView mav = new ModelAndView();
Map model = new HashMap();
Person person = new Person();
person.setBirthday(new Date());
person.setPersonName("zhoufeng");
person.setPersonId(1);
model.put("person", person);
mav.addAllObjects(model);
mav.setViewName("index");
return mav;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)