Spring MVC的使用

来源:互联网 发布:苹果数据线线序 编辑:程序博客网 时间:2024/06/06 08:29

Spring MVC的hello world!

本次使用maven项目;

项目结构:


pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.meter</groupId><artifactId>myshiro</artifactId><packaging>war</packaging><version>0.1</version><name>myshiro</name><url>http://maven.apache.org</url><repositories><repository><id>mvn</id><url> http://central.maven.org/maven2/ </url></repository><repository><id>cloudera</id><url>https://repository.cloudera.com/artifactory/cloudera-repos/</url></repository><repository><id>cloudera-repo-releases</id><url>https://repository.cloudera.com/content/repositories/releases</url></repository></repositories><dependencies><!-- logback日志 --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId><version>1.2.2</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.2</version></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-access</artifactId><version>1.2.2</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.21</version></dependency><!-- jcl-over-slf4j <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.21</version> </dependency> --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency><!--commons-logging --><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><!-- druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.28</version></dependency><!-- mysql-jdbc --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><!-- spring-core --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.3.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.3.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.3.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.3.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.3.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.3.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>4.3.7.RELEASE</version></dependency></dependencies><build><finalName>myshiro</finalName></build></project>
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"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>myshiro_spring</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:application*.xml</param-value></context-param><listener><description>spring入口</description><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><listener><description>spring自带解决内存溢出</description><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener><filter><description>系统日志记录</description><filter-name>sysFilter</filter-name><filter-class>org.meter.demo.filter.SysFilter</filter-class></filter><filter-mapping><filter-name>sysFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><filter><description>spring解决乱码问题</description><filter-name>encoding</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>encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping><servlet><description>spirngMVC的入口</description><servlet-name>springMvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springMvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>

spring配置:

<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 使用外部属性文件配置 --><context:property-placeholderignore-unresolvable="true" location="classpath*:/application.properties" /><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="maxActive" value="${jdbc.druid.maxActive}" /><property name="ValidationQuery" value="select 'X' " /><property name="minIdle" value="${jdbc.druid.minIdle}" /><property name="maxWait" value="${jdbc.druid.maxWait}" /><property name="initialSize" value="${jdbc.druid.initialSize}" /></bean></beans>


mvc配置:

<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><mvc:annotation-driven /><mvc:resources location="/static/" mapping="/static/**"/><context:component-scan base-package="org.meter.demo.web" /><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/demo"></property><property name="suffix" value=".jsp" /></bean></beans>


配置过程中遇到的问题:

注解不生效:是因为没有添加:<mvc:annotation-driven />

静态文件报错404:<mvc:resources location="/static/" mapping="/static/**"/>参考:http://blog.csdn.net/figo645/article/details/50403872




原创粉丝点击