springMVC 注解版本的工程搭建,web工程名字为:springMVC-study
1、首先在web.xml中加入springMVC的前端过滤器,DispatcherServlet. 这里的servlet-name就规定了springmvc的配置文件名字为springmvc-servlet.xml。
<!--配置一个控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup><!-- 表示在容器启动时就加载 -->
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
2
、新建一个
springmvc
配置文件,名字为:
springmvc-servlet.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:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 启用注解 -->
<context:annotation-config/>
<!-- 自动扫描所有的包 -->
<context:component-scan base-package="com.springmvc"/>
<mvc:annotation-driven />
<!--Spring3.1开始的注解 HandlerMapping -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!--Spring3.1开始的注解 HandlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 解析页面的规则 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/> <!--表示:/jsp/list.jsp -->
</bean>
<!-- 配置加载静态文件 -->
<mvc:resources location="/js/" mapping="/js/**"/>
</beans>
3
、编写一个前端控制器
UserController
,在
Controller
中的方法返回值不一定为
ModelAndView
,可以是任意的对象
package com.springmvc.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import com.springmvc.domain.User;
@Controller //@Controller这个注解用来标记这是个控制器,
@RequestMapping("/user")//@RequestMapping这个注解可以标记在类上也可以标记在方法上,用来映射类或者方法
public class UserController {
/*跳转到register.jsp*/
@RequestMapping("/addUI")
public ModelAndView addUserUI(){
ModelAndView mv=new ModelAndView();
/*ModelAndView是一个类似于Map的,可以通过addObject("key","value"),这种方式去放值,在jsp页面可以通过EL表达式${key}拿到value*/
mv.addObject("key", "admin");
mv.setViewName("register");
return mv;
}
/*处理前台提交的参数,并跳转到list.jsp*/
@RequestMapping(value="/add",method=RequestMethod.POST)
public ModelAndView addUser(User user){
ModelAndView mv=new ModelAndView();
mv.addObject("user","admin");
mv.setViewName("list");
return mv;
}
/*springMVC与Ajax整合*/
@RequestMapping(value="/ajax",method=RequestMethod.POST)
@ResponseStatus(OK)//
/*@ResponseBody该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区
*返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;
*/
@ResponseBody
public Map<Object, Object> spring_ajax(){
Map<Object, Object> map=new HashMap<Object, Object>();
map.put("name", "admin");
map.put("age", 1);
map.put("email", "admin2014@.com");
return map;
}
}
这里用到一个
User
对象
package com.springmvc.domain;
public class User{
private Integer id;
private String userName;
private String age;
private String password;
private String email;
.....set与get方法......
}
4 、编写项目所需要的页面 register.jsp 与 list.jsp ,我们在 WebContent 下面建立一个 jsp 的 folder, 将 register.jsp 与 list.jsp 放入其中,然后在 WebContent 下再建立一个 js 文件夹,把 jquery-1.11.1.js 文件放入。
register.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户注册</title>
</head>
<body><center>
<form action="${pageContext.request.contextPath}/user/add" method="POST">
用户名: <input type="text" name="userName"/><br>
用户年龄:<input type="text" name="age"><br>
用户密码:<input type="password" name="password"/><br>
用户邮箱:<input type="text" name="email"><br>
<input type="submit" value="注册"/>
</form>
</center></body>
</html>
list.jsp 页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>List User </title>
</head>
<body>
<%=request.getSession().getAttribute("user")%><br>
This is list User page!
<br><input type="button" value="ajax" id="btn"/>
<div id="div1"></div>
<script type="text/javascript" src="../js/jquery-1.11.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function(){
$.ajax({
url:'./ajax',
type:'POST',
success:function(data){
$("#div1").text(data.name+" "+data.age+" "+data.email);
},
error:function(data){
alert("fail");
}
});
});
});
</script>
</body>
</html>
5
、通过访问
:http://localhost:8080/springMVC-study/user/addUI
可以直接跳到
register.jsp
页面,按要求填好参数,点击注册按钮可以跳转到
list.jsp
页面。