SPRING MVC3.2案例讲解--SPRING MVC3的各种URL映射+JSP视图跳转(3)

本章节主要讲解使用springmvc进行controller到view的转向,涉及代码包含JSP视图,

 

 

 

和上一章节最大的配置点不同是:

 

无任何视图指向的Controller   @ResponseBody是关键代码,表示直接返回内容,不转向JSP视图

@Controller
public class MappingController {

	@RequestMapping("/mapping/path")
	public @ResponseBody String byPath() {
		return "Mapped by path!";
	}
}

 

指向具体视图的Controller,无@ResponseBody,表示会指向某个JSP视图

@Controller
@RequestMapping("/views/*")
public class ViewsController {

	@RequestMapping(value="html", method=RequestMethod.GET)
	public String prepare(Model model) {
		model.addAttribute("foo", "bar");
		model.addAttribute("fruit", "apple");
		return "views/html";
	}

 

基于视图转向的四种用法:

1.自己制定转向的视图

	//  http://127.0.0.1:8010/views/html --->对应 views/html.jsp
	@RequestMapping(value="html", method=RequestMethod.GET)
	public String prepare(Model model) {
		model.addAttribute("foo", "bar");
		model.addAttribute("fruit", "apple");
		return "views/html";
	}
	

 

 

2.spring指向默认的视图,通过org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator实现默认视图的转向,见日志信息:

DEBUG DispatcherServlet Unable to locate RequestToViewNameTranslator with name 'viewNameTranslator': using default [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@55a58f]

//  http://127.0.0.1:8010/views/viewName --->对应 views/viewName.jsp
	@RequestMapping(value="/viewName", method=RequestMethod.GET)
	public void usingRequestToViewNameTranslator(Model model) {
		model.addAttribute("foo", "bar");
		model.addAttribute("fruit", "apple");
	}
	

 

3.URI的 @PathVariable 的获取,注意参数名称必须一致!如:

 

//  http://127.0.0.1:8010/views/pathVariables/bar/apple --->对应 views/html.jsp
	@RequestMapping(value="pathVariables/{foo}/{fruit}", method=RequestMethod.GET)
	public String pathVars(@PathVariable String foo, @PathVariable String fruit) {
		// No need to add @PathVariables "foo" and "fruit" to the model
		// They will be merged in the model before rendering
		return "views/html";
	}
 

 

 

4. 利用URL的@PathVariable给JAVABEAN赋值 

 

//http://127.0.0.1:8010/views/dataBinding/bar/apple --->对应 views/dataBinding.jsp
	@RequestMapping(value="dataBinding/{foo}/{fruit}", method=RequestMethod.GET)
	public String dataBinding(@Valid JavaBean javaBean, Model model) {
		// JavaBean "foo" and "fruit" properties populated from URI variables 
		return "views/dataBinding";
	}
 
public class JavaBean {
	
	@NotNull
	private String foo;

	@NotNull
	private String fruit;
//注意此处有GET SET 方法,篇幅有限,不在文章中赘诉
}
 群:J2EE系统架构  203431569 ,进群前请标注开发的软件所属行业及工作经验!!

 

  • 大小: 70.7 KB
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要在Spring MVC配置相应的控制器和视图解析器。 在Spring配置文件中添加以下配置: ```xml <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> ``` 这里我们使用JSP作为视图模板,所以需要配置视图解析器,将JSP文件映射到对应的控制器方法。 接下来,我们编写登陆验证的控制器。在控制器中,我们需要处理登陆请求并进行验证。如果验证通过,则跳转到欢迎页面;否则跳转到注册页面。 ```java @Controller public class LoginController { @RequestMapping(value = "/login", method = RequestMethod.POST) public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model) { // 实现登录验证逻辑 if (/*验证通过*/) { // 跳转到欢迎页面 return "welcome"; } else { // 跳转到注册页面 return "register"; } } } ``` 在上面的代码中,我们使用`@RequestParam`注解来获取用户提交的用户名和密码。然后,我们可以根据需要执行验证逻辑。如果验证通过,我们将返回欢迎页面;否则返回注册页面。 接下来,我们需要在JSP页面中提供相应的表单,让用户输入用户名和密码并提交登陆请求。我们可以使用以下代码实现: ```html <form method="post" action="/login"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required><br> <label for="password">密码:</label> <input type="password" id="password" name="password" required><br> <button type="submit">登陆</button> </form> ``` 在上面的代码中,我们使用HTML表单来收集用户输入的用户名和密码,并将其提交到我们定义的登陆控制器中进行处理。 最后,我们需要编写欢迎页面和注册页面的JSP模板,以显示相应的内容。 欢迎页面的模板可以使用以下代码: ```html <h1>欢迎您!</h1> ``` 注册页面的模板可以使用以下代码: ```html <h1>注册</h1> <form method="post" action="/register"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required><br> <label for="password">密码:</label> <input type="password" id="password" name="password" required><br> <button type="submit">注册</button> </form> ``` 在上面的代码中,我们使用HTML表单来收集用户输入的用户名和密码,并将其提交到注册控制器中进行处理。 综上所述,我们已经实现了一个基本的注册和登陆功能。用户可以通过登陆页面输入用户名和密码进行登陆验证,并根据验证结果跳转到欢迎页面或注册页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值