一直做Android前端开发,想学学后台J2EE服务器开发 的知识,零基础第一步学习一个简单例子:
一, demo结构:
数据库:
二, SpingMVC框架:
拷贝相应的jar到lib纹路下:
三, 在myeclipse中添加Spring支持:
右键点击该工程,在对话框中选择“MyEclipse->Add Spring Capabilities...”,添加Spring,并进行相关配置,如图4所示,采用默认配置即可。本例选用的spring3.1。
Spring以及Hibernate配置中图片参考别人demo的配置,跟本例在女文件目录,文件名等方面不一致(仅作参考),具体数据库反向等操作根据自己建立的数据库连接文件来。
配置文件:web.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
主要指定相应的servlet。
配置文件:spring-mvc :
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/context/spring-mvc-3.2.xsd">
没有用注解,主要指定控制模块,urlmapping以及识图渲染。
配置文件:applicationContext.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
org.hibernate.dialect.MySQLDialect
主要配置数据源,sessionfactory等以及具体操作的bean。
五,相应的逻辑实现:
Account.java ,AccountDAO以及Account.hbm.xml为数据表反向自动生成;
控制模块实现简单逻辑:
LoginController.java:
package com.oliver.web.controller;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import com.oliver.dao.Account;
import com.oliver.dao.AccountDAO;
public class LoginController extends AbstractController {
private String successView;
private String failView;
public String getSuccessView() {
return successView;
}
public void setSuccessView(String successView) {
this.successView = successView;
}
public String getFailView() {
return failView;
}
public void setFailView(String failView) {
this.failView = failView;
}
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
String username=request.getParameter("username");
String password=request.getParameter("password");
Account account =getListAccount(username);
Map
if(account !=null){
model.put("account", account);
return new ModelAndView(getSuccessView(),model);
}else{
model.put("error", "卡号和密码不正确");
return new ModelAndView(getFailView(),model);
}
}
@SuppressWarnings("unchecked")
public Account getListAccount(String username){
Account thisAccount=null;
//从src/applicationContext.xml装载BeanFactory
Resource resource=new ClassPathResource("config/applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
//从BeanFactory获取UserDAO
AccountDAO accountDAO = (AccountDAO) factory.getBean("AccountDAO");
//添加新User
List accountlist=accountDAO.findByUsername(username);
Iterator
while(itaccount.hasNext()){
Account myaccount=itaccount.next();
thisAccount=myaccount;
}
return thisAccount;
}
}
本例仅仅是联系demo,读取数据表中的内容(只有一项内容)所以才那么写。
六, 测试:
浏览器输入 :
http://localhost:8090/MvcSpringTest/pages 即可进入相应的demo功能测试。
具体实例源码下载链接:
http://download.csdn.net/detail/ccm_oliver/8311087