springMVC01,使用xml

时间:2021-05-01 19:38:07

unit03_01

unit03_02

springmvc流程图

springMVC01,使用xml

案例:hello示例

/hello.do --> springmvc --> /WEB-INF/hello.jsp

/hello.do

--> DispatcherServlet

-->HandlerMapping

-->HelloController

-->ViewResolver

-->hello.jsp

a。搭建Spring Web MVC开发环境

--引入ioc,webmvc开发包

--src下添加applicationContext.xml

b。编写HelloController

2.显示登录页面

/tologin.do

-->DispatcherServlet

-->handlermapping

-->ToLoginController

-->ViewResolver

-->/WEB-INF/login.jsp

3.单机登陆按钮处理

/login.do(提交表单)

-->DispatcherServlet

-->handlermapping

-->LoginController(获取提交的用户名和密码,如果用户名为root,密码为1234,正确)

-->ViewResolver

-->错误进入/WEB-INF/login.jsp。正确进入/WEB-INF/ok.jsp

完整代码:

spring 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"

xmlns:util="http://www.springframework.org/schema/util"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!-- 配置handlermapping -->

<bean id="handlermapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<!-- 指定请求和controller对应关系 -->

<property name="mappings">

<props>

<prop key="/hello.do">helloController</prop>

<prop key="/tologin.do">tologinController</prop>

<prop key="/login.do">loginController</prop>

</props>

</property>

</bean>

<!-- 配置controller -->

<bean id="helloController" class="org.alexhe.controller.HelloController"></bean>

<bean id="tologinController" class="org.alexhe.controller.ToLoginController"></bean>

<bean id="loginController" class="org.alexhe.controller.LoginController"></bean>

<!-- 配置ViewResolver -->

<bean id="viewresolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

</beans>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

<display-name>dnspringmvc1</display-name>

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<servlet>

<description>JAX-RS Tools Generated - Do not modify</description>

<servlet-name>JAX-RS Servlet</servlet-name>

<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>JAX-RS Servlet</servlet-name>

<url-pattern>/jaxrs/*</url-pattern>

</servlet-mapping>

</web-app>

HelloController.java:

package org.alexhe.controller;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.Controller;

public class HelloController implements Controller{

@Override

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

ModelAndView mav=new ModelAndView();

mav.setViewName("hello");//设置视图名字,也就是hello.jsp 从spring的xml里prefix,suffix加上

mav.getModel().put("msg", "模型数据");//等价于request.setAttribute("msg","data");

return mav;

}

}

ToLoginController.java:

package org.alexhe.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class ToLoginController implements Controller{

@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav=new ModelAndView();
mav.setViewName("login");//设置视图名字,也就是hello.jsp 从spring的xml里prefix,suffix加上

return mav;
}

}

LoginController.java:

package org.alexhe.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class LoginController implements Controller{

@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav=new ModelAndView();
String username=request.getParameter("username");
String password=request.getParameter("password");
if("root".equals(username)&&"1234".equals(password)){
mav.setViewName("ok");//设置视图名字,也就是hello.jsp 从spring的xml里prefix,suffix加上
}else{
mav.setViewName("login");
}
return mav;
}

}

login.jsp:

<form action="login.do" method="post">

用户名:<input type="text" name="username"><br>

密码:<input type="password" name="password"><br>

<input type="submit" value="登陆">

</form>

ok.jsp:略

hello.jsp:略