Spring框架web项目实战全代码分享

网友投稿 398 2023-03-13


Spring框架web项目实战全代码分享

以下是一个最简单的示例

1、新建一个标准的javaweb项目

2、导入spring所需的一些基本的jar包

3、配置web.xml文件

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

contextConfigLocation

classpath*:applicationContext*.xml,

org.springframework.web.context.ContextLoaderListener

index.jsp

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

contextConfigLocation

classpath*:applicationContext*.xml,

org.springframework.web.context.ContextLoaderListener

index.jsp

4、添加spring配置文件applicationContext

5、对applicationContext.xml文件做最简单的配置

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd"

default-lazy-init="false" default-autowire="byName">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd"

default-lazy-init="false" default-autowire="byName">

beans——xml文件的根节点。

xmlns——是XMLNameSpace的缩写,因为XML文件的标签名称都是自定义的,自己写的和其他人定义的标签很有可能会重复命名,而功能却不一样,所以需要加上一个namespace来区分这个xml文件和其他的xml文件,类似于java中的package。

xmlns:xsi——是指xml文件遵守xml规范,xsi全名:xmlschemainstance,是指具体用到的schema资源文件里定义的元素所准守的规范。即/spring-beans-2.0.xsd这个文件里定义的元素遵守什么标准。

xsi:schemaLocation——是指,本文档里的xml元素所遵守的规范,schemaLocation属性用来引用(schema)模式文档,解析器可以在需要的情况下使用这个文档对XML实例文档进行校验。它的值(URI)是成对出现的,第一个值表示命名空间,第二个值则表示描述该命名空间的模式文档的具体位置,两个值之间以空格分隔。

6、新建一个实体类User.java

package com.po;

public class User {

private String name;

private String age;

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;

}

}

7、测试

public static void main(String[] args) {

// TODO Auto-generated method stub

ApplicationContext ac = new FileSystemXmlApplicationContext("config/applicationContext.xml");

User user =(User)ac.getBean("user");

System.out.println(user.getName());

}

输出

这就实现web项目搭建基础spring框架。接下来就做一些真正项目中会用到的一些扩展

可以在web.xml中配置一些spring框架集成的功能或其他设置

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

forceEncoding

true

encoding

UTF-8

encodingFilter

/*

openSessionInViewFilter

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

singleSession

true

sessionFactoryBeanName

sessionFactory

openSessionInViewFilter

/*

springSecurityFilterChain

org.springframework.web.filter.DelegatingFilterProxy

springSecurityFilterChain

/*

springDispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath*:spring-mvc.xml

1

springDispatcher

/

404

errorpage/404.jsp

401

/errorpage/401.html

/WEB-INF/runqianReport4.tld

/WEB-INF/runqianReport4.tld

*.jsp

UTF-8

/tag/taglib.jspf

其中jspf就是做一些全局的声明

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<%@ taglib prefix="fnc" uri="/WEB-INF/tlds/fnc.tld" %>

<%@ taglib tagdir="/WEB-INF/tags" prefix="mytag"%>

value="${pageContext.request.contextPath}" />

可以在applicationContext.xml中配置更多的功能

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:jee="http://springframework.org/schema/jee"

xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/jee http://springframework.org/schema/jee/spring-jee.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd"

default-lazy-init="false" default-autowire="byName">

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

classpath:jdbc.properties

destroy-method="close">

1

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

classpath*:/com/schoolnet/**/*.hbm.xml

${jdbc.username}

org.hibernate.dialect.Oracle10gDialect

true

false

auto

true

org.hibernate.cache.EhCacheProvider

false

false

update

thread

class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />

advice-ref="txAdvice" />

spring-mvc.xml文件配置

xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/jee http://springframework.org/schema/jee/spring-jee.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">

总结

以上就是本文关于Spring框架web项目实战全代码分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

springmvc Rest风格介绍及实现代码示例

SpringMVC拦截器实现单点登录

Spring集成Redis详解代码示例

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

value="${pageContext.request.contextPath}" />

可以在applicationContext.xml中配置更多的功能

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:jee="http://springframework.org/schema/jee"

xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/jee http://springframework.org/schema/jee/spring-jee.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd"

default-lazy-init="false" default-autowire="byName">

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

classpath:jdbc.properties

destroy-method="close">

1

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

classpath*:/com/schoolnet/**/*.hbm.xml

${jdbc.username}

org.hibernate.dialect.Oracle10gDialect

true

false

auto

true

org.hibernate.cache.EhCacheProvider

false

false

update

thread

class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />

advice-ref="txAdvice" />

spring-mvc.xml文件配置

xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/jee http://springframework.org/schema/jee/spring-jee.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:jee="http://springframework.org/schema/jee"

xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/jee http://springframework.org/schema/jee/spring-jee.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd"

default-lazy-init="false" default-autowire="byName">

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

classpath:jdbc.properties

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

classpath:jdbc.properties

destroy-method="close">

1

destroy-method="close">

1

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

classpath*:/com/schoolnet/**/*.hbm.xml

${jdbc.username}

org.hibernate.dialect.Oracle10gDialect

true

false

auto

true

org.hibernate.cache.EhCacheProvider

false

false

update

thread

class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />

advice-ref="txAdvice" />

spring-mvc.xml文件配置

xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/jee http://springframework.org/schema/jee/spring-jee.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

classpath*:/com/schoolnet/**/*.hbm.xml

${jdbc.username}

org.hibernate.dialect.Oracle10gDialect

true

false

auto

true

org.hibernate.cache.EhCacheProvider

false

false

update

thread

class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />

class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />

advice-ref="txAdvice" />

spring-mvc.xml文件配置

xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/jee http://springframework.org/schema/jee/spring-jee.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">

advice-ref="txAdvice" />

spring-mvc.xml文件配置

xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/jee http://springframework.org/schema/jee/spring-jee.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">

xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context" xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/jee http://springframework.org/schema/jee/spring-jee.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.0.xsd">

总结

以上就是本文关于Spring框架web项目实战全代码分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

springmvc Rest风格介绍及实现代码示例

SpringMVC拦截器实现单点登录

Spring集成Redis详解代码示例

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java接口设计文档模板(java接口总结)
下一篇:微服务api网关服务(微服务网关gateway简书)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~