Eclipse maven New SPRINGMVC Project (original)

Source: Internet
Author: User

1. Configure Eclipse maven

2. New MAVEN Project

3, new Src/main/java, update 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>exam</groupId> <artifactId>exam_3</artifactId> <packaging >war</packaging> <version>0.0.1-SNAPSHOT</version> <name>exam_3 Maven webapp</name > <url>http://maven.apache.org</url>  <dependencies> <dependency> <groupId>junit</groupId> <artifactid>junit</ar        tifactid> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-web</artif actid> <version>3.0.5.RELEASE</version> </dependency> <dependency> < Groupid>org.springframework</groupid> <artifactId>spring-webmvc</artifactId> <versio n>3.0.5.release</version> </dependency> <dependency> <groupid>org.apache.geron Imo.specs</groupid> <artifactId>geronimo-servlet_2.5_spec</artifactId> <version>1.2& lt;/version> </dependency> </dependencies> <build> <finalname>exam_3</fi Nalname> <plugiNs> <plugin> <!--compilation Plugin--<groupid>org.apache.maven.plugins </groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2                    .3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> &LT;PL                ugin> <!--source plug-in <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-source-plugin</artifactId> <version>2.1</version>                        <!--automatically publish the source at the same time-<executions> <execution> <id>attach-sources</id> <goals> <goal&gt       ;jar</goal>                 </goals> </execution> </executions> </p lugin> <plugin> <groupId>org.mortbay.jetty</groupId> &lt ;artifactid>maven-jetty-plugin</artifactid> <version>6.1.26</version> &                      Lt;configuration> <webAppSourceDirectory>src/main/webapp</webAppSourceDirectory> <contextPath>/exam_3</contextPath> <scanintervalseconds>10</scaninte rvalseconds> <connectors> <connector implementation= "Org.mortbay.jet Ty.nio.SelectChannelConnector "> <port>5555</port> &lt                ;maxidletime>100</maxidletime> </connector> </connectors> </configuration> </plugin> </plugins> </build></project> 
View Code

4. Update Project maven Dependency

5. Write Web. xml

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > <context-param> &lt ;p aram-name>contextconfiglocation</param-name> <param-value>classpath:applicationContext.xml< /param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <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>ch Aracterencodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>exam</servlet-nam E> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init- Param> <param-name>contextConfigLocation</param-name> <PARAM-VALUE>CLASSPATH:SP ring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <serv let-name>exam</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <w Elcome-file-list> <welcome-file>index.jsp</welcome-file> </WELCOME-FILE-LIST></WEB-APP&G t;
View Code

6, write Src/main/resources directory under Applicationcontext.xml and Spring-mvc.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" ><beans ></beans>
View Code
<?xml version= "1.0" encoding= "UTF-8"?><!--Bean head--><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:mvc= "Http://www.springframework.org/schema/mvc" xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:util= "Http://www.springframework.org/schema/util"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp//Www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp//Www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-3.0.xsd"><!--activate @controller mode--<mvc:annotation-driven/> <!--scan all classes in the package to complete the function of bean creation and automatic dependency injection needs to be changed-- <context:component-scan base- Package= "Com.demo.web.controller"/> <beanclass= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <bean id= "Viewresolver"class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" > <value>/WEB-INF/jsp/</value> </property> <property name= "suffix" > <val Ue>.jsp</value> </property> </bean></beans>
View Code

7, new part of the code

 Public classLoginForm {PrivateString username; PrivateString password;  PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }}
View Code
 Public class Logincontroller {     @RequestMapping (value= "Logind")     public  Modelandview Login (httpservletrequest request,httpservletresponse response,loginform command) {         =  Command.getusername ();          New Modelandview ("/index/index", "command", "LOGIN SUCCESS," + username);          return mv;     } }
View Code

index.jsp login.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" >      </body > 
View Code
<%@ 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" >
View Code

Root directory index.jsp

<%     request.getrequestdispatcher ("/web-inf/jsp/login/login.jsp"). Forward (request,response);% >
View Code

8. Directory Structure

9. Running the project

Ok.......................... (reproduced please specify the source, thank you ^_-)

Eclipse maven New SPRINGMVC Project (original)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.