`
86asm
  • 浏览: 199814 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

spring-mvc入门(五):使用注解(下)

 
阅读更多

 

2.类级别的基路径请求

在上面的实例中,我们通过为方法配置请求路径来进行访问,而下面我们将为类配置一个请求实例,这种类似于struts2package-namespace

控制器代码如下:package com.asm;

//...省略导入的相关类
@Controller
@RequestMapping("/myRoot")
public class AnnotationControl {
	@RequestMapping(value = "/the/{name}.do")
	public String getName(@PathVariable
	String name, Model model) {
		model.addAttribute("message", "名字:" + name);
		return "anno";
	}

	@RequestMapping("/age.do")
	public ModelAndView getAge(@RequestParam
	int age) {
		ModelAndView mav = new ModelAndView("anno");
		mav.addObject("message", "年龄:" + age);
		return mav;
	}
}

 1)访问getAge方法

这样如果想访问到上面的两个方法都必须是以myRoot作为开头。下面我们先对getAge方法进行访问测试index.jsp页面增加如下代码:

   <form action="<%=request.getContextPath() %>/myRoot/age.do" method="post">

    <input type="text" name="age">

    <input type="submit" value="提交年龄">

      </form>

这样提交请求时,将会访问到getAge方法,并且age会作为参数传递给此方法,这样我们便可以得到客户端输入的age @RequestParam注解:Annotation which indicates that a method parameter should be bound to a web request parameter意为此注解将会为方法中的参数绑定对应(对应是指名字上,比如这里表单和方法中参数名都用了age)的web请求参数。强调:访问路径必须是以myRoot作为一个基路径,因为类上配置了请求基路径。@RequestParam注解实现web请求到方法参数的绑定。

2)访问getName方法

对于getName方法它的访问路径应该是这样的:…/myRoot/the/jack.do 这样我们得到@PathVariable Annotation which indicates that a method parameter should be bound to a URI template variable,意思是此注解将会把一个uri路径中对应的变量和方法中的参数绑定,这里我们用jack来代替来{name},所以实质就是把jack作为参数和方法中name参数绑定,如果想传递其它值,只须把jack换下就可以了,比如:…/myRoot/the/tom.do等。

 

3.自动表单

struts框架中,表单参数能成功地交给struts Action来处理,在前面的实例中我们使用springmvc也完成了类似功能,下面我们使用spring mvc注解方式来完成表单参数和业务层实体的绑定。

步骤一、准备业务层实体Bean,User.java主要代码如下:

 

package com.asm;
public class User {
	private String username;
	private String password;
//省略getter/setter方法。
}

 

步骤二:编写控制层代码

package com.asm;
//省略导入的相关类
@Controller
public class FormAnnotationControl {
	@ModelAttribute("user")
	public User initUser() {
		User user = new User();
		user.setUsername("在此处填写用户名");
		return user;
	}

	@RequestMapping("reg.do")
	public String addUI() {
		return "reg";
	}

	@RequestMapping("save.do")
	public String add(@ModelAttribute
	User user, Model model) {
		model.addAttribute(user);
		return "userInfo";
	}

	@RequestMapping("login.do")
	public ModelAndView login(@ModelAttribute
	User user) {
		ModelAndView mav = new ModelAndView(new RedirectView("manage.do"));
		if (!"admin".equals(user.getUsername())) {
			mav = new ModelAndView("error");
		}
		return mav;
	}

	@RequestMapping("manage.do")
	public String manage() {
		return "list";
	}
}

 说明:@ModelAttribute Annotation that binds a method parameter or method return value to a named model attribute, exposed to a web view. Supported for RequestMapping annotated handler classes,此注解可以用于web请求参数和方法参数的绑定,也可用于方法的返回值作为模型数据(这种模型数据在类中全局有效,比如这里initUser方法绑定的模型数据在此类的任何方法中都能访问到这种模型数据)。

步骤三、编写相关的jsp页面

reg.jsp

  <form action="<%=request.getContextPath() %>/save.do" method="post">

    用户名:<input type="text" name="username" value="${user.username }"><br/>

    密 码:  <input type="password" name="password" value="${user.password }"><br/>

    <input type="submit" value="保存用户">

   </form>

userInfo.jsp
 
<body>

    保存用户成功,信息如下:<br/>

    用户名:${user.username}<br/>

    密 码:${user.password}

  </body>

list.jsp

<body>

    列出所有用户

</body>

error.jsp

<body>

    用户你好,你没权力进入后台

</body>

步骤四、输入相关信息进行测试。

reg.do,对应于addUI()方法的访问:我们输入..../reg.do,可以看到页面显示如下内容:

 

“在此处填写用户名”,这几个字是由于我们在reg.jsp中使用了${user.usernam},并且在initUser方法中我们通过ModelAttribute注解初始化了一个模型数据,这样我们便可以在此类对应的任何方法映射的视图中访问到此模型数据,这也即是前面提到这种模型数据在类中全局有效。然后填写相关数据,提交表单给sava.do,即是提交给给save方法,在save方法中我们同样使用了ModelAttribute注解,此注解在save方法中实现了web请求参数和方法参数的绑定的功能,也即是说把webusernamepasswordModelAttribute指示的user中的usernamepassword进行绑定,这种绑定和struts2中的模型驱动ModerDriven极为相似。

步骤五、访问登录页面,在login方法中我们对登录进行了简单的校验,如果用户名是admin则允许后台进行管理操作,如果不是则提示用户没有权力进行此操作。在前面我们对save方法进行了简要分析,这里的login方法也是类似得。另在此方法中如果是admin登录我们重定向到管理页面,如果不是admin登录,我们使用forward进行转发。访问login.dojsp页面代码如下:

<form action="<%=request.getContextPath() %>/login.do" method="post">

    <input type="text" name="username">

    <input type="submit" value="登入管理后台">

</form>

<!--[endif]-->

5
3
分享到:
评论
2 楼 86asm 2011-12-12  
crocx 写道
关于步骤五,最后 我觉得按正确逻辑的话 应该是,是admin forward的到管理页面,不是就重定向吧?

在实际中,如果是类似登录之类的表单,如果填写OK最好都重定向一次要好些。
1 楼 crocx 2011-12-12  
关于步骤五,最后 我觉得按正确逻辑的话 应该是,是admin forward的到管理页面,不是就重定向吧?

相关推荐

Global site tag (gtag.js) - Google Analytics