1. 程式人生 > >springmvc02,使用註解

springmvc02,使用註解

ava ack set 業務 modelmap 流程 對象 resource turn

unit04_01

a.RequestMappingHandlerMapping組件

@RequestMapping("/login.do")

該標記用在Controller業務方法前

b.Controller編寫和配置

取消了實現Controller接口及方法的約定,可以允許程序猿按需要靈活定義業務方法.

Controller需要掃描到Spring容器,[email protected]

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)

[email protected]("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方法參數

[email protected]("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){

}


案例:列表顯示功能

/list.do

-->DispatcherServlet

-->Handlermapping

-->ListController,傳遞到list.jsp

-->ViewResolver

-->/WEB-INF/list.jsp

springmvc02,使用註解