Hefei Programmer Group: 49313181. Hefei Real-name programmer Group: 128131462 (do not want to disclose names and information to join)
Q q:408365330 E-mail:[email protected]
Review:
I haven't done web development in a while, and I've been working on Android and iOS for a while, but there's a project that needs to think about cross-platform and have to do server-side interfaces with Java Web. No way, just started to write code that will do is ASP. NET Web development, now only gig. The Java language is quite familiar (Android development has been in use). But Java web development needs to master the technology is quite a lot. With my previous experience in ASP. NET development, there are several frameworks, MVC frameworks, ORM Tools, IOC and AOP frameworks to choose from. First of all, people especially like the MVC pattern, so it is necessary to choose an MVC framework (since someone else has already made the wheel, it will not be silly to implement the MVC pattern). Java Open source ecosystem, the choice of room is very large, more famous than Struts2 and Springmvc. I chose Springmvc. There's no other reason, just that it's small, easy-to-use performance is pretty good, and it's seamlessly connected to spring's IOC framework. ORM I consider choosing MyBatis, needless to say everyone knows its advantages, semi-automatic ORM, performance must be Good o IOC and AOP Framework no choice, spring. After all, in the ASP. NET ecosystem, suddenly to the Java Web Ecosystem, you have to make some records, first record the SPRINGMVC configuration and the first program. and then it hellowold.
SPRINGMVC Configuration and First Helloword
First, import the jar package:
Eclipse and Tomcat configuration and integration are not logged. Start by creating a new Web project in MyEclipse (for the time being, you don't need maven to manage the Project jar package, all manually to configure and import).
For example: The red circle is for us to manually add, config directory is put SPRINGMVC related configuration. Lib is where we put the jar package, and views are the view layers in MVC, which is also the directory of the viewing files.
By copying these jar packages into the Lib directory, MyEclipse will automatically help us with the myeclipse10.x.
II. Web. XML configuration
1 <!--Scheduling with servlet -2 <servlet>3 <Servlet-name>Dispatcherservlet</Servlet-name>4 <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>5 <Init-param>6 <Param-name>Contextconfiglocation</Param-name>7 <Param-value>Web-inf/config/spring-mvc.xml</Param-value>8 </Init-param>9 <Load-on-startup>1</Load-on-startup>Ten </servlet> One <!--URL Mapping - A <servlet-mapping> - <Servlet-name>Dispatcherservlet</Servlet-name> - <Url-pattern>/</Url-pattern> the </servlet-mapping> - <servlet-mapping>
The point is that the configuration, first of all, is to configure a servlet to handle client requests, so what is the request being handled by the servlet? The answer is yes,
< servlet-mapping > < Servlet-name > Dispatcherservlet</servlet-name> <Url-pattern >/</url-pattern></servlet-mapping >
<url-pattern>/</url-pattern> decided that all the files were handled by this servlet. But we were puzzled that our static files such as. Jpg,.js.css these files, of course, have it handled. The client must not be able to request, don't worry, we have a way to deal with it later. The bridge to the bow is naturally straight.
< Load-on-startup >1</load-on-startup>
This does not explain too much, it is understood to have this configuration it and the Tomcat environment before the start.
< Param-name >contextconfiglocation</param-name><Param-value >web-inf/config/spring-mvc.xml</param-value>
This configuration is very clear, this is the configuration Spingmvc file, this is why we manually add the Config directory, we will put SPRINGMVC configuration in this directory.
Iii. configuration of SPRINGMVC configuration file Spring-mvc.xml
Create this configuration file in the Config directory. Add the following code:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"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"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 Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!--Configuring the Scan controller package - <Context:component-scanBase-package= "Com.egojit" /> <!--Configuring the return type - <Mvc:annotation-driven> <mvc:message-convertersRegister-defaults= "true"> <Beanclass= "Org.springframework.http.converter.StringHttpMessageConverter"> < Propertyname= "Supportedmediatypes"> <List> <value>Text/plain;charset=utf-8</value> <value>Text/html;charset=utf-8</value> </List> </ Property> </Bean> </mvc:message-converters> </Mvc:annotation-driven> <!--resolution of view names - <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"P:order= "+"P:viewclass= "Org.springframework.web.servlet.view.JstlView"P:prefix= "/web-inf/views/"P:suffix= ". jsp" /> </Beans>
Do not explain too much, there are comments in the code.
<!--- <class = " Org.springframework.web.servlet.view.InternalResourceViewResolver " p:order=" P:viewclass = "Org.springframework.web.servlet.view.JstlView" p:prefix= "/web-inf/views/" p:suffix= ". jsp"/>
To explain this, this is the C-level directory that configures MVC, which is the directory path of the view view. And configured the back pendant is. jsp
D. Code controller and HelloWorld:
First Look carefully:
Package Com.egojit; Import Org.springframework.stereotype.Controller; Import org.springframework.web.bind.annotation.RequestMapping; @Controller Public class testcontroller{ @RequestMapping ("/test/index") private String Index () { return "OK"; }}
I created a controller by annotating it.
And the URL is mapped by the @requestmapping ("/test/index") annotation, which corresponds to the URL Http://localhost:8080/test3/test/index
Return "OK" means that the ok.jsp file in the Views view directory is returned (see red circle). There is a perfect helloworld to show in front of us. Such as
So that SPRINGMVC can be used, will use the back in-depth is not divided into minutes of things,
Postscript:
If you have any questions, I hope you will correct me, compare younger brother is the first time to do Java Web development. Case program can be csdn in my resources to download, no points required. The jar package and source code are all on the top, click Transfer
springmvc4.x Configuration of Java Web