首页 > 代码库 > springmvc02,使用注解
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:
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #4e9192 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #4f76cb } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #3933ff } span.s1 { color: #000000 } span.s2 { color: #009193 } span.s3 { color: #932192 } span.s4 { color: #3933ff } span.s5 { color: #4e9192 } span.Apple-tab-span { white-space: pre }</style><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:
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #4e9192 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco } span.s1 { color: #000000 } span.s2 { color: #009193 } span.s3 { text-decoration: underline; color: #000000 } span.s4 { color: #4e9192 } span.Apple-tab-span { white-space: pre }</style><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:
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; min-height: 16.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #777777 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #931a68 } span.s1 { color: #931a68 } span.s2 { color: #000000 } span.s3 { color: #3933ff } span.s4 { color: #7e504f } span.Apple-tab-span { white-space: pre }</style>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:
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco } span.s1 { color: #009193 } span.s2 { color: #4e9192 } span.s3 { text-decoration: underline }</style><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:
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #3933ff } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco } span.s1 { color: #000000 } span.s2 { color: #009193 } span.s3 { color: #4e9192 } span.s4 { color: #932192 } span.Apple-tab-span { white-space: pre }</style><form action="login.do" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登陆">
</form>
LoginController:
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; min-height: 16.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #777777 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #4e9072 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #931a68 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #3933ff } span.s1 { color: #931a68 } span.s2 { color: #000000 } span.s3 { color: #3933ff } span.s4 { color: #7e504f } span.Apple-tab-span { white-space: pre }</style>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。
例子:
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #777777 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #3933ff } span.s1 { color: #000000 } span.s2 { color: #3933ff } span.s3 { color: #931a68 } span.s4 { color: #7e504f } span.s5 { color: #0326cc } span.Apple-tab-span { white-space: pre }</style>@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";
}
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #777777 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #3933ff } span.s1 { color: #000000 } span.s2 { color: #3933ff } span.s3 { color: #931a68 } span.s4 { color: #7e504f } span.s5 { color: #0326cc } span.Apple-tab-span { white-space: pre }</style>@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";
}
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #4e9072 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #777777 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #3933ff } span.s1 { color: #000000 } span.s2 { color: #3933ff } span.s3 { color: #931a68 } span.s4 { color: #7e504f } span.s5 { color: #0326cc } span.Apple-tab-span { white-space: pre }</style>//测试用实体对象接收请求参数,前台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的例子:
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #777777 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #7e504f } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #931a68 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #3933ff } span.s1 { color: #000000 } span.s2 { color: #3933ff } span.s3 { color: #931a68 } span.s4 { color: #7e504f } span.Apple-tab-span { white-space: pre }</style>@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){
}
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #777777 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco } span.s1 { color: #000000 } span.s2 { color: #3933ff } span.s3 { color: #931a68 } span.s4 { color: #7e504f } span.Apple-tab-span { white-space: pre }</style>例子:
@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
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco; color: #4e9072 } span.s1 { color: #000000 } span.s2 { color: #7e504f } span.s3 { color: #3933ff } span.s4 { text-decoration: underline } span.Apple-tab-span { white-space: pre }</style>
springmvc02,使用注解