springmvc02,使用注解

unit04_01

a.RequestMappingHandlerMapping组件

@RequestMapping("/login.do")

该标记用在Controller业务方法前

 

b.Controller编写和配置

取消了实现Controller接口及方法的约定,可以允许程序猿按需要灵活定义业务方法.

Controller需要扫描到Spring容器,必须使用@Controller

public ModelAndView或者String 方法名(根据需要定义request,session,response)

例子:

public String execute(){

  return "hello";

}

public ModelAndView execute(){

  ModelAndView mav=new ModelAndView();

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

  return "hello";

}

 

流程:

-->DispatcherServlet

-->HandlerMapping

-->HelloController

-->ViewResolver

-->hello.jsp

 

完整代码:

spring xml:

<mvc:annotation-driven/>

<!-- 扫描controller -->

<context:component-scan base-package="org.alexhe"></context:component-scan>

<!-- 配置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>

web.xml:

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

HelloController.java:

package org.alexhe.controller;

 

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

@Controller

public class HelloController {

@RequestMapping("/hello.do")

public ModelAndView execute(){

ModelAndView mav=new ModelAndView();

mav.setViewName("hello");

mav.getModel().put("msg", "我是一个msg");

return mav;

}

}

hello.jsp:

<h2>springmvc注解版,${msg}</h2>


 

unit04_02

进入登陆页面

/tologin.do

-->DispatcherServlet

-->Handlermapping

-->Logincontroller.toLogin

-->ViewResolver

-->login.jsp

 

/login.do

-->DispatcherServlet

-->Handlermapping

-->Logincontroller.checkLogin

-->ViewResolver

-->错误login.jsp,正确ok.jsp

完整代码:

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>

LoginController:

package org.alexhe.controller;

 

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

public class LoginController {

@RequestMapping("/tologin.do")

public String toLogin(){

return "login";//返回视图名称

}

@RequestMapping("/login.do")

public String checkLogin(HttpServletRequest request){

//获取请求信息

String name=request.getParameter("username");

String password=request.getParameter("password");

if("root".equals(name)&&"1234".equals(password)){

return "ok";

}else{

return "login";

}

 

}

}

 


 

1.如何接受请求参数

  a。利用HttpServletRequest

  b。利用业务方法参数

    --参数名与请求参数key保持一致,public String f1(String username,String password)

    --利用@RequestParam("key"),public String f1(@RequestParam("username")String name,@RequestParam("password")String pass)//前台jsp传过来的是username和password

  c。利用实体对象参数

使用建议:少量参数用b。大量参数用c。遇到非字符串类型参数建议使用a。

例子:

@RequestMapping("/login1.do")

public String checkLogin1(String username,String password){

System.out.println("执行checklong1方法");

System.out.println("username:"+username);

System.out.println("password:"+password);

return "login";

}

@RequestMapping("/login2.do")

public String checkLogin2(@RequestParam("username")String name,@RequestParam("password")String passw){

System.out.println("执行checklong2方法");

System.out.println("username:"+name);

System.out.println("password:"+pass);

return "login";

}

//测试用实体对象接收请求参数,前台jsp不需要像struts一样改成user.username。还是用username和password

@RequestMapping("/login2.do")

public String checkLogin2(User user){

System.out.println("执行checklong2方法");

System.out.println(user.getUsername());

return "login";

}

 

 

2.Controller如何向相应jsp传值

a。利用HttpServletRequest

b。利用ModelAndView做返回值

c。利用ModelMap方法参数

d。使用@ModelAttribute("key")

c的例子:

@RequestMapping("/login3.do")

public String checkLogin3(String username,String password,ModelMap model){

if("root".equals(username)&&"1234".equals(password)){

model.put("user", username);

return "ok";

}else{

model.put("msg","用户名密码错误");

return "login";

}

}

 

3.Controller如何使用Session

--

public String checkLogin(HttpServletRequest request){

}

或者干脆直接加HttpSession

public String checkLogin(HttpSession session){

}

例子:

@RequestMapping("/login3.do")

public String checkLogin3(String username,String password,ModelMap model,HttpSession session){

}

 


 

案例:列表显示功能 (unit0501 1:25:00)

/list.do

-->DispatcherServlet

-->Handlermapping

-->ListController,传递到list.jsp

-->ViewResolver

-->/WEB-INF/list.jsp

 


4如何解决中文接收乱码问题

mysql jdbc utf-8

request.setCharacterEncoding("utf-8");

filter拦截器里加request.setCharacterEncoding("utf-8");

filter例子:

web.xml里加:

<filter>

  <filter-name>myfilter</filter-name>

  <filter-class>org.alexhe.filter.MyFilter</filter-class>

  </filter>

  <filter-mapping>

  <filter-name>myfilter</filter-name>

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

  </filter-mapping>

MyFilter类:

//每次的拦截方法

@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

throws IOException, ServletException {

// TODO Auto-generated method stub

//前期逻辑会走

chain.doFilter(request, response);//调用servlet,放行

//后面的逻辑,也会走

}

除了上面这个方法,还有spring-webjar包里有很多filter,listener组件:

不用自己写myfilter类,并把web.xml改成:

   <filter>

  <filter-name>myfilter</filter-name>

  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

  <init-param>

  <param-name>encoding</param-name>

  <param-value>UTF-8</param-value>

  </init-param>

  </filter>

  <filter-mapping>

  <filter-name>myfilter</filter-name>

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

  </filter-mapping>

如果get请求有中文,需要改tomcat的配置文件。 

 

5.如何处理异常

测试类Controller,访问他肯定报空指针异常:

@Controller

public class ExceptionController1 {

@RequestMapping("/exception1.do")

public String ex1(){

String s =null;

s.length();

return "ok";

}

}

只要在applicationContext.xml加:这就是全局异常

<!-- 异常处理器 -->

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

<property name="exceptionMappings">

<props>

<prop key="java.lang.Exception">error</prop>  <!-- 加上前缀后缀,找WEB-INF/error.jsp ,可以多加几个prop,针对不同异常指定不同的相应页面-->

</props>

</property>

</bean>

这样访问http://localhost:8080/dnspringmvc2/exception1.do就会跳到报错页面了

 

第二种方法:每个controller最好都加,执行该方法后,不会再调用ExceptionResolver

//局部当前Controller异常处理,只限于当前Controller出现异常

@ExceptionHandler

public String handleException(HttpServletRequest request,Exception e){

request.setAttribute("msg", "带出去的消息");

return "error";

}

 

第三种方法:常用于测试阶段,把异常集中写入文件。

spring的xml,需要注释第一种方法的全局简单异常:

<!-- 使用自定义ExceptionResolver -->

<bean class="org.alexhe.exception.MyExceptionHandler">

</bean>

MyExceptionHandler.java:

public class MyExceptionHandler implements HandlerExceptionResolver{

 

@Override

public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object arg2,

Exception e) {

// 异常写入文件

  System.out.println(e);

  //返回错误页面

ModelAndView mav=new ModelAndView();

mav.setViewName("error");

return mav;

}

 

}

 

6.如何实现登录权限检查

使用session进行约定值判断。

实现方法:1.采用Filter。2.采用拦截器

a拦截器组件简介

拦截器是springmvc的组件

拦截器可以在Controller之前或者之后拦截;

也可以在jsp解析完毕给浏览器输出之前拦截。

b.拦截器的使用方法

首先编写一个拦截器组件(实现HandlerInterceptor),在约定方法中添加要插入的逻辑,然后在applicationContext.xml中配置一下。

 spring.xml:

<!-- 配置拦截器 -->

<mvc:interceptors>

<mvc:interceptor>

<!-- 拦截哪些请求 -->

<mvc:mapping path="/tologin.do"/>

<mvc:mapping path="/tohello.do"/>

<!-- <mvc:mapping path="/**"/> 表示全都拦下 /*只拦一层 -->

<!-- 放过哪些请求 -->

<mvc:exclude-mapping path="/tologin.do"/>

<bean class="org.alexhe.interceptor.LoginInterceptor"></bean>

</mvc:interceptor>

</mvc:interceptors>

拦截器.java:

public class LoginInterceptor implements HandlerInterceptor {

//请求处理完毕,输出之前

public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)

throws Exception {

}

 

//Controller之后

public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)

throws Exception {

}

 

//请求处理之前,Controller之前

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {

//实现登陆检查

HttpSession session=request.getSession();

//获取登陆成功后放置的用户信息。

String name=(String)session.getAttribute("username");

if(name!=null){//登陆成功

return true;//继续执行mvc后续流程

}else{//未登陆

response.sendRedirect("toLogin.do");

return false;

}

//return false;//记得改成true

}

 

}

 

 

 

 

posted @ 2017-05-08 22:31  alexhe  阅读(252)  评论(0编辑  收藏  举报