注解方式的login案例
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>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value> 默认
</init-param>
-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
</web-app>
spring-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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 启用spring mvc 注解 -->
<context:annotation-config />
<!-- 设置使用注解的类所在的jar包 -->
<context:component-scan base-package="com.asm"></context:component-scan>
<!-- 完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 测试Service -->
<bean id="loginService" class="com.asm.LoginService"></bean>
</beans>
TestController
package com.asm;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
//import entity.User;
@Controller //类似Struts的Action
public class TestController {
@RequestMapping("/login.do") // 请求url地址映射,类似Struts的action-mapping
public String testLogin(@RequestParam(value="username")String name, String password, HttpServletRequest request) {
// @RequestParam一般用于当表单的请求参数名和方法中形参不一致时,将指定的请求参数付给方法中形参
// @RequestParam可简写为:@RequestParam("username")
request.setAttribute("username", name);
request.setAttribute("password", password);
if (!"admin".equals(name) || !"admin".equals(password)) {
return "failure"; // 跳转页面路径(默认为转发),该路径不需要包含spring-servlet配置文件中配置的前缀和后缀
}
return "success";
}
@RequestMapping("/login2.do")
public ModelAndView testLogin2(String username, String password,int age,HttpServletRequest request){
// request和response不必非要出现在方法中,如果用不上的话可以去掉
// 参数的名称是与页面控件的name相匹配,参数类型会自动被转换
request.setAttribute("username", username);
request.setAttribute("password", password);
request.setAttribute("age", age);
if ("admin".equals(username) &&"admin".equals(password)) {
return new ModelAndView("success"); // 手动实例化ModelAndView完成跳转页面(转发),效果等同于上面的方法返回字符串
}
return new ModelAndView(new RedirectView("./index.jsp")); // 采用重定向方式跳转页面
// 重定向还有一种简单写法
// return new ModelAndView("redirect:../index.jsp");
}
@RequestMapping("/login3.do")
public ModelAndView testLogin3(User user) {
// 同样支持参数为表单对象,User类似于Struts的ActionForm,User不需要任何配置,直接写即可,放到request内置对象中。
String username = user.getUsername();
String password = user.getPassword();
int age = user.getAge();
if (!"admin".equals(username) || !"admin".equals(password) ) {
return new ModelAndView("error");
}
return new ModelAndView("success");
}
@Resource(name = "loginService") // 获取applicationContext.xml中bean的id为loginService的对象,并注入给下面标识的对象
private LoginService loginService; //等价于spring传统注入方式写get和set方法,这样的好处是简洁工整,省去了不必要得代码
@RequestMapping("/login4.do")
public String testLogin4(User user) {
if (loginService.login(user) == false) {
return "error";
}
return "success";
}
}
login1知识点
1、tomcat一启动,读取web.xml,创建监听器对象,appication一创建,就把全局参数封装到application,里取得配置信息,创建bean工厂,
创建DispatcherServlet对象,执行init方法,读取主配信息,创建web工厂,里面要创建三个对象
(1)创建请求分派器对象(请求适配器AnnotationMethodHandlerAdapter),
(2)创建后端控制器对象。启动注解,扫描包,到对应的包下找到注解为@Controller 标识的类,创建这个类的对象
(3)创建视图定位器对象。
2、浏览器发请求,请求一提交,tomcat就创建request对象封装请求参数,一看是.do的请求,就交给DispatcherServlet。DispatcherServlet一拿到这个请求,就进行截取,截取成“/login.do”,到web工厂里找请求分派器,找到后端控制器,根据 @RequestMapping注解把请求关联到action(后端控制器、业务层前端控制器)对应的方法上。
一调用这个方法,往方法里注入数据,把表单里的参数名和处理请求的方法的形参联系。如果一样,直接赋值;如果不一样,通过注解@RequestParam把表单里的参数值注入给形参。
返回一个字符串,返回给DispatcherServlet,从web工厂里拿出视图定位器,给字符串加上前缀和后缀,通过服务端跳转到装配的页面
login2知识点
- 如果表单里的请求参数类型和方法里的形参不一致,它能自动进行转换
- 可以通过
new ModelAndView(new RedirectView("./index.jsp"));
进行客户端跳转,不经过请求分派器
login3知识点
自动创建user对象,自动把表单里的同名参数封装到user对象的同名属性。类型不同会自动转换。然后自动把封装表单参数的user对象设到request内置对象里。属性名user,属性值user对象。
login4知识点
tomcat一启动首先创建监听器对象,对application对象创建做监听,application对象一创建就会自动把全局初始化参数封装到application对象里,然后监听器监听到application对象创建就调用Contextinitlize(),通过事件对象拿到application,从application对象里取得全局的初始化参数,通过参数名拿到参数值。拿到参数值后到WEB-INF下拿到applicationContext.xml配置信息,创建bean工厂,一创建bean工厂,业务对象就创建了,然后把bean工厂设到application里。
接着创建DispatcherServlet对象,通过读取servlet-name找到主配文件,创建web工厂。一创建web工厂就会创建三个对象,
(1)请求分派器
(2)后端控制器。通过注解找到后端控制器对象
(3)视图定位器
一创建后端控制器对象,给loginService属性(实例全局变量)注入数据,这个数据来自注解,从bean工厂里拿到id为loginService对象注入给后端控制器的loginService属性上
web工厂:创建请求分派器,视图定位器,后端控制器,异常处理器,上传分配器等
bean工厂:
网友评论