maven springmvc环境配置

来源:互联网 发布:淘宝店铺被删除保证金 编辑:程序博客网 时间:2024/05/17 04:51

1,建立maven工程,webapp


2,group id artifact id 


3,新建source folder

4,


5,设置output folder 



6,设置/src/test/resources, /src/test/resources,的output folder为test-classes


7,最后的工程结构


8,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>springmvc-chapter2</display-name>    <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>          /WEB-INF/ContextLoaderListener.xml      </param-value></context-param>   <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- POST中文乱码过滤器 -->    <filter>        <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <servlet>        <servlet-name>springtest3</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>       <!-- 查找springtest3-servlet.xml -->        <servlet-name>springtest3</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>        </web-app>

9,ContextLoaderListener.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"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd"></beans>

10,springtest3-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"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd">    <!-- HandlerMapping -->    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>        <!-- HandlerAdapter -->    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>        <!-- ViewResolver -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>        <property name="prefix" value="/WEB-INF/jsp/"/>        <property name="suffix" value=".jsp"/>    </bean>        <!-- 处理器 -->    <bean name="/hello" class="com.cn.myspring.controller.HelloWorldController"/>    </beans>

11,index.jsp

<html><body><h2>START SPTING3.0! IN INDEX.JSP</h2><a href="hello">"href=hello"</a></body></html>

12,hello.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>MEIZU MX3 </title></head><body>${message} in HELLO.JSP</body></html>

13,HelloWorldController.java

package com.cn.myspring.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;//实现Controller接口public class HelloWorldController implements Controller {public ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {ModelAndView mv = new ModelAndView();mv.addObject("message", "你好,WELCOME  Spring MVC!");mv.setViewName("spring");return mv;}}

14,最终的POM文件:

<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.my.springmvc</groupId><artifactId>springtest3</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>springtest1 Maven Webapp</name><url>http://maven.apache.org</url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>3.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>3.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>3.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>3.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>3.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-asm</artifactId><version>3.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>3.0.5.RELEASE</version></dependency><!-- end spring 核心依赖包 --><!-- start spring mvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>3.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>3.0.5.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!-- START SERVLET --><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.1</version><scope>provided</scope></dependency></dependencies><build><finalName>springtest1</finalName><plugins><plugin><groupId>org.mortbay.jetty</groupId><artifactId>maven-jetty-plugin</artifactId><version>6.1.25</version><configuration><reload>manual</reload><scanIntervalSeconds>0</scanIntervalSeconds><contextPath>/</contextPath><!-- <webDefaultXml>src/main/resources/webdefault.xml</webDefaultXml> --><connectors><connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"><port>8080</port><maxIdleTime>60000</maxIdleTime></connector></connectors></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.1</version><configuration><source>1.5</source><target>1.5</target><encoding>UTF-8</encoding><!-- <failOnError>false</failOnError> --></configuration></plugin></plugins></build></project>

15,最红的文件结构式这样的(这个地方蛋疼了一小下,居然把jsp下载webapp下,好惊悚,NOT_FOUND):


这个时候就可以运行了:

Run As --->Run Jetty 

可以通过浏览器访问了:


跳转至hello.jsp



原创粉丝点击