您的位置:首页 > 编程语言 > Java开发

springMVC环境搭建-4

2016-01-22 17:37 381 查看
配置注解,实现页面跳转

新建包路径com.wf.login.controller(路径自定义),新建文件LoginController.java,

package com.wf.login.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/login")
public class LoginController {
@RequestMapping("/tologinview")
public ModelAndView toLoginView(){
ModelAndView mav = new ModelAndView();
//jsp页面路径前缀和后缀已经配置在springmvc.xml中了
mav.setViewName("login/login");
return mav;
}
}

在WEB-INF下新建jsp/login/login.jsp,作为跳转页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'login.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
This is my JSP page. <br>
</body>
</html>


配置springmvc.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<!-- 添加Controller遍历路径 --><!-- 模糊匹配 -->
<context:component-scan base-package="com.wf.**.controller"></context:component-scan>
<!-- 采用注解驱动方式配置适配器和映射器 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 添加视图解析器 --><!-- 配置路径的前缀和后缀,配置后可省略路径中的前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>


最后进行测试,启动项目,输入地址:http://localhost:8080/Dest/login/tologinview
进入login.jsp页面则表示配置成功了...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: