spring+springmvc的例子

1、spring版本:spring(4.2.6)

2、字体设置:

Window->preferences->appearance->colors and fonts->

3、Utf-8设置:

 Window->Preferences->General->Content Type->Text->JSP 最下面设置为UTF-8

 Window->Preferences->General->Workspace  面板Text file encoding 选择UTF-8

 Window->Preferences->Web->JSPFiles 面板选择 ISO 10646/Unicode(UTF-8)

4、目录展示:

5、jar包的展示:

6、web.xml配置:

 

<?xmlversion="1.0" encoding="UTF-8"?>

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

 xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

 id="WebApp_ID"version="3.0">

 <display-name>SringMvc_Extjs_Demo</display-name>

 

<!--设定配置文件列表 设置全局参数 为listener的参数配置信息classpath*:/config/springAnnotation-hibernate.xml -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

classpath*:/config/springAnnotation-servlet.xml

</param-value>

</context-param>

<!--注册配置文件读取器,监听spring配置文件的变化 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!--编码字符集统一为UTF-8(过滤器) -->

<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>

<init-param>

<param-name>forceEncoding</param-name>

<param-value>true</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!--应用中使用了OpenSessionInViewFilter或者OpenSessionInViewInterceptor,所有打开的session会被保存在一个线程变量里

这主要是为了实现Hibernate的延迟加载功能采用了spring的声明式事务模式,它会对你的被代理对象的每一个方法进行事务包装(AOP的方式) -->

<!--<filter> <filter-name>openSession</filter-name><filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>

</filter><filter-mapping> <filter-name>openSession</filter-name><url-pattern>/*</url-pattern>

</filter-mapping>-->

<!--配置spring分发器(是总的控制中心 被拦截的url会汇聚到该servlet) -->

<servlet>

<servlet-name>spring-servlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!--可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml-->

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:/config/springAnnotation-servlet.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<!--配置spring拦截的url模板以.do、.html等结尾的url -->

<servlet-mapping>

<servlet-name>spring-servlet</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

7、springAnnotation-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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
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/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
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 注解扫描包 -->
<context:component-scan base-package="com.gree"></context:component-scan>
<!--启动注解功能 -->
<mvc:annotation-driven></mvc:annotation-driven>
    <!-- 静态资源访问 -->
<mvc:resources location="/Resource/image/" mapping="/Resource/image/**"></mvc:resources>
<mvc:resources location="/Resource/js/" mapping="/Resource/js/**"></mvc:resources>
<mvc:resources location="/Resource/pagejs/" mapping="/Resource/pagejs/**"></mvc:resources>
<mvc:resources location="/Resource/css/" mapping="/Resource/css/**"></mvc:resources>
<!-- 定义跳转的文件的前后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://10.2.4.232:3306/machine?useUnicode=true&amp;characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>

8、页面代码:

 

<!DOCTYPE HTMLPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

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

pageEncoding="UTF-8"%>

<metahttp-equiv="Content-Type" content="text/html;charset=UTF-8">

<title>DEMO</title>

<linktype="text/css" rel="stylesheet"href="Resource/css/demo.css"></link>

<link

href="Resource/script/extjs-5.1/ext-theme-classic/build/resources/ext-theme-classic-all.css   rel="stylesheet"/>

<scriptsrc="Resource/script/extjs-5.1/ext-all.js"></script>

<scriptsrc="Resource/script/extjs-5.1/ext-locale/ext-locale-zh_CN.js"></script>

<scripttype="text/javascript"src="Resource/script/jquery/jquery.min.js"></script>

</head>

<body>

<form>

<h1>测试代码</h1>

<inputtype="button" value="Test" id="btnWater"class="btn">

</form>

</body>

</html>

猜你喜欢

转载自blog.csdn.net/nannan1232/article/details/78105187
今日推荐