刚到公司入职,开发框架使用的是SpingMVC,之前并没有接触过,所以需要提前学习下如何使用,花了这两天的时间,看了一些文档和一些源码,加上各种百度需要的方法说明,终于是搭建起了自己的小demo。
以下是环境搭建用的jar包:
com.springsource.javax.servlet.jsp.jstl-1.1.2.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.taglibs.standard-1.1.2.jar
org.springframework.aop-3.0.0.RELEASE.jar
org.springframework.asm-3.0.0.RELEASE.jar
org.springframework.beans-3.0.0.RELEASE.jar
org.springframework.context-3.0.0.RELEASE.jar
org.springframework.context.support-3.0.0.RELEASE.jar
org.springframework.core-3.0.0.RELEASE.jar
org.springframework.expression-3.0.0.RELEASE.jar
org.springframework.web-3.0.0.RELEASE.jar
org.springframework.web.servlet-3.0.0.RELEASE.jar
我没有连接数据库,所以一些数据库的驱动包是没有的^_^
首先,建立好包结构,如下
接下来开始jdbc-servlet.xml(其实就是spring的applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 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" default-autowire="byName"> <!--数据源、事物的配置也放在这里,因为我的demo没放数据库,所以就偷懒不写啦--> <!--扫描组建--> <context:component-scan base-package="cn.wy.test"> <!--把Controller屏蔽掉,省的冲突--> <context:exclude-filter type="regex" expression="cn.wy.test.action"/> </context:component-scan> </beans>
当然还要写spring-mvc的配置文件呀,spring-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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <!--当然是来扫描Controller的--> <context:component-scan base-package="cn.wy.test"> <context:exclude-filter type="regex" expression="cn.wy.test.dao" /> <context:exclude-filter type="regex" expression="cn.wy.test.service" /> <context:exclude-filter type="regex" expression="cn.wy.test.entity" /> </context:component-scan> <!--自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean, 是spring MVC为@Controllers分发请求所必须的。--> <mvc:annotation-driven /> <!--REST风格用来屏蔽静态文件路径--> <!-- <mvc:resources location="" mapping=""/> --> <!-- <mvc:interceptors> <mvc:interceptor></mvc:interceptor> </mvc:interceptors> --> <!-- <mvc:view-controller path="" view-name=""/> --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views" p:suffix=".jsp"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> </beans>
最后就是web.xml了
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>spring_mvc_demo</display-name> <!--配置父容器--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/jdbc-servlet.xml</param-value> </context-param> <!--配置spring监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--字符编码过滤--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置springMVC的容器(子容器)--> <servlet> <servlet-name>spring_mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring_mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/WEB-INF/index.jsp</welcome-file> </welcome-file-list> </web-app>
实体类:
package cn.wy.test.entity;
public class UserInfoPO {
private long id;
private String name;
private String age;
private String comeFrom;
private String work;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getComeFrom() {
return comeFrom;
}
public void setComeFrom(String comeFrom) {
this.comeFrom = comeFrom;
}
public String getWork() {
return work;
}
public void setWork(String work) {
this.work = work;
}
}
DAO接口:
package cn.wy.test.dao;
import cn.wy.test.entity.UserInfoPO;
public interface IUserInfoDao {
public UserInfoPO getUser();
}
DAO实现类:
package cn.wy.test.dao.impl;
import org.springframework.stereotype.Repository;
import cn.wy.test.dao.IUserInfoDao;
import cn.wy.test.entity.UserInfoPO;
@Repository("userDao")
public class UserInfoDao implements IUserInfoDao {
@Override
public UserInfoPO getUser() {
UserInfoPO user=new UserInfoPO();
user.setName("**");
user.setAge("24");
user.setComeFrom("大海");
user.setWork("lol");
user.setId(1);
return user;
}
}
Service接口:
package cn.wy.test.service;
public interface IUserInfoService {
public String getInfo();
}
Service实现类:
package cn.wy.test.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.wy.test.dao.IUserInfoDao;
import cn.wy.test.entity.UserInfoPO;
import cn.wy.test.service.IUserInfoService;
@Service("userService")
public class UserInfoService implements IUserInfoService {
@Autowired
private IUserInfoDao userDao;
@Override
public String getInfo() {
UserInfoPO u=new UserInfoPO();
u=userDao.getUser();
return u.getId()+":我的名字叫"+u.getName()+",我今年"+u.getAge()+"岁,来自遥远的"+u.getComeFrom()+",我现在从事着IT行业,是一名"+u.getWork()+",谢谢。";
}
}
Controller类:
package cn.wy.test.action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
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.servlet.ModelAndView;
import cn.wy.test.entity.UserInfoPO;
import cn.wy.test.service.IUserInfoService;
@Controller
@RequestMapping("/user")
public class UserInfoController {
@Autowired
private IUserInfoService userService;
@RequestMapping(value = "/jieshao.do"/**,method=RequestMethod.POST*/)
public ModelAndView jieshao(@RequestParam(value = "login") String login,@ModelAttribute(value="user") UserInfoPO user) {
System.out.println(user.getName()+","+user.getAge());
ModelAndView mav = new ModelAndView();
mav.setViewName("/jieshao");
mav.addObject("jieshao", userService.getInfo());
return mav;
}
}
HTML
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Insert title here</title>
</head>
<body>
<a href='<c:url value="/user/jieshao.do?login=123"/>'>sussess</a>
<form method="post" action='<c:url value="/user/jieshao.do"/>'>
用户名<input name="name"/><br/>
年龄<input name="age" type="text"/><br/>
<input type="submit" value="open"/>
</form>
</body>
</html>
<%@ 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>Insert title here</title>
</head>
<body>
${jieshao }<br/>
${user.name } ${user.age }
</body>
</html>
部署一下就可以用了。
最后写一下对于SpringMVC的一点点理解吧:
1)因为使用了jpa,使得配置文件简化了很多
2)Controller不需要像以前写struts2的时候配置许多的配置文件用来引导,采用mapping的方式更加让人一目了然
3)页面表单的提交,采用@ModelAttribute的注解方式,可以使页面传值自动和实体类的属性对应
4)采用了父子容器的方式搭建出来的环境,需要注意区分哪些组件在那里扫描,否则会产生冲突