SpringMVC+Maven的配置。
来源:互联网 发布:数据库实例不可用 编辑:程序博客网 时间:2024/04/29 22:46
SpringMVC+Maven工程构建步骤。
1.将apache-maven-3.0.3下载并解压至文件夹。
2.在eclipse中选择windows->preferences->Maven->Installations 在右边的栏中选择add,将apache-maven-3.0.3加入进去即可。
3.新建工程开始前选择Windows-Preferences-Workspace右栏的Text file encoding选择other utf-8. Content Types的右栏全部改成utf-8..
选择File->new->Maven Project,在第二个next的时候,选择org.apache.maven.archetypes / maven-archetype-webapp,此时会包一个javax的错误,在poen.xml中加入如下代码。
<span style="white-space:pre"></span><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version></dependency>
当然在配置的时候要加入其他的dependency,基础的spring框架,在添加完代码后需要,右键项目,Run as->Maven install...
要使用jetty运行项目的话,需要添加如下代码:
<build><plugins><plugin><artifactId>maven-surefire-plugin</artifactId><configuration><skip>true</skip></configuration></plugin><plugin><groupId>org.mortbay.jetty</groupId><artifactId>maven-jetty-plugin</artifactId></plugin></plugins><defaultGoal>compile</defaultGoal></build>4.配置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" version="2.5"><display-name>Archetype Created Web Application</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml,/WEB-INF/test-servlet.xml</param-value></context-param><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><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><!-- 此处的*.html可以替换成/*,*do等。这里配置成什么样的Controller中的@RequestMapping(value = "*.html") --><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>*.html</url-pattern></filter-mapping><servlet><servlet-name>test</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/test-servlet.xml</param-value></init-param><load-on-startup>2</load-on-startup></servlet><!-- servletname跟你新建的servletname-servlet要一致 --><servlet-mapping><servlet-name>test</servlet-name><url-pattern>*.html</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file><welcome-file>default.jsp</welcome-file><welcome-file>index.html</welcome-file></welcome-file-list></web-app>5.由于已经设定了servletname为test,则在WEB-INF目录下新建test-servlet.xml文件,若没有指定名字,则新建dispatch-servelt。该文件配置如下:
<?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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsd"><!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --><context:component-scan base-package="com.test.web"/><!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --><mvc:annotation-driven/><mvc:default-servlet-handler/> <!-- ③:对模型视图名称的解析,即在模型视图名称添加前后缀需要在WEB-INF下新建jps文件夹,并将jsp文件放入其中 --><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix"><value>/WEB-INF/jsp/</value></property><property name="suffix"><value>.jsp</value></property></bean></beans>6.src/main/resources目录下新建applicationContext.xml和jdbc.properties文件。文件配置如下:
applicationCintext.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: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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" default-autowire="byName"><!-- 使类中定义的注解(@。。。 )起作用 --><!-- 扫描com.test.dao包下所有标注@Repository的DAO组件 --><context:component-scan base-package="com.test.dao"/><!-- 扫描com.test.service包下所有标注@Service的服务组件 --><context:component-scan base-package="com.test.service"/><!-- 数据访问层配置 --><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:jdbc.properties"/></bean><!-- 定义一个使用DBCP实现的数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}"/><!-- jdbcTemplate注入 --><!-- 定义JDBC模版bean --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource"><ref bean="dataSource"/></property></bean></beans>jdbc.properties文件内容如下:
#MySQLjdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://192.168.1.204:3306/maizuo?useUnicode=true&characterEncoding=UTF-8jdbc.username=webjdbc.password=1234567.在src/main/java目录下,新建com.test.dao,com.test.domin,com.test.service,com.test.web.controller。如果说,只要一个输出helloworld的话。只要在controller下,新建一个LoginController。文件内容如下:
package com.test.web.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping("test")public class LoginController {@RequestMapping(value = "test1.html")public ModelAndView test1(){ModelAndView mv = new ModelAndView();//此处的mav是你jsp文件夹的名字。mv.setViewName("mav");mv.addObject("mavs", "你是谁?");return mv;}}8.在web-inf->jsp下新建mav.jsp内容如下。
<%@ 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>${mavs} </body></html>9.运行,run as -> maven build.在goals中输入jetty:run -Djetty.port=80。
10.在浏览器中输入localhost/example/test/test1.html即可显示我是谁?,此处的example是工程名,test,test1是controller中的。
@RequestMapping("test"),<pre name="code" class="java">@RequestMapping(value = "test1.html")另外我做了一个基于tUser表的增删改查,是基于MySQL数据库的。也有用到json.
http://download.csdn.net/detail/cheng1111111/7857893
当映射为@RequestMapping("/user/add")时,为例:
1、拦截*.do、*.htm, 例如:/user/add.do
这是最传统的方式,最简单也最实用。不会导致静态文件(jpg,js,css)被拦截。
2、拦截/,例如:/user/add
可以实现现在很流行的REST风格。很多互联网类型的应用很喜欢这种风格的URL。
弊端:会导致静态文件(jpg,js,css)被拦截后不能正常显示。想实现REST风格,事情就是麻烦一些。后面有解决办法还算简单。
3、拦截/*,这是一个错误的方式,请求可以走到Action中,但转到jsp时再次被拦截,不能访问到jsp。
0 0
- SpringMVC+Maven的配置。
- SpringMVC的maven配置
- Maven配置SpringMVC的POM文件
- 基于Maven项目SpringMVC的基本配置
- maven+springmvc+dubbo的简单配置
- eclipse 创建maven的 springmvc配置
- idea15 springmvc mybatis maven 的安装,配置
- SpringMVC+mybatis的配置(依赖maven)
- SpringMVC开发一:SpringMVC的maven搭建及配置
- maven springmvc环境配置
- maven springmvc环境配置
- Springmvc配置(maven项目)
- springmvc开发,maven配置
- maven springmvc环境配置
- SpringMvc Maven基本配置
- maven-springmvc配置
- SpringMVC+MyBatis+MAVEN配置
- Springmvc maven环境配置
- 音乐播放器
- WSAStartup与WSACleanup
- android解决三星手机拍照获取图片的问题
- EditText杂知识
- smartforms 我见
- SpringMVC+Maven的配置。
- ARM处理器中“8位位图”
- 抽象类调用抽象方法的问题
- 尿毒症患者的常见症状
- MVC三级联动无刷新
- Split Ruby array into sub array with slash
- 【面试准备】letcode-Tow Sum
- sqlserver从入门到精通
- IOS生成证书