spring学习笔记-----(MVC学习)

1、首先配置web.xml文件,用来处理页面请求。
<servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>*.do</url-pattern>
</servlet-mapping>

2、配置mvc-config.xml文件
<beans>   
    <bean name="/hello.do"
          class="onlyfun.caterpillar.HelloController">
        <property name="viewPage">
            <value>/hello.jsp</value>
        </property>
    </bean>
</beans>
3、配置处理类(控制器)
所有的控制器,也就是action必需实现org.springframework.mvc.Controller接口,或者继承AbstractController抽象类。当继承时,需要实现handleRequestInternal(HttpServletRequest,HttpServletRespoonse)抽象方法。
public class LoginAction implements  Controller {

 private String viewPage="hello";
 public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws
Exception {
  String user=req.getParameter("username");
  Map map=new HashMap();
  map.put("qqq",user);

  return new ModelAndView(viewPage,"user",map);

 }
 public void setViewPage(String viewPage) {
  this.viewPage = viewPage;
 }
}


你可能感兴趣的:(spring)