Spring MVC: A simple example of using simpleurlhandlermapping
Source: Internet
Author: User
<span id="Label3"></p><p><p>Implement a controller Shirdrncontroller as Follows:</p></p><p><p>Package org.shirdrn.spring.mvc;</p></p><p><p>Import java.util.Date;</p></p><p><p>Import javax.servlet.http.HttpServletRequest;<br>Import javax.servlet.http.HttpServletResponse;</p></p><p><p>Import org.apache.commons.logging.Log;<br>Import org.apache.commons.logging.LogFactory;<br>Import org.springframework.web.servlet.ModelAndView;<br>Import org.springframework.web.servlet.mvc.Controller;</p></p><p><p>public class Shirdrncontroller implements Controller {</p></p><p><p>Public Modelandview HandleRequest (httpservletrequest arg0,<br>HttpServletResponse Arg1) throws Exception {<br>Log logger = Logfactory.getlog (getclass ());<br>Date date = new Date ();<br>Logger.info ("info: [" +date.tolocalestring () + "] calls the controller shirdrncontroller, and turns to the index.jsp page.");<br>return new Modelandview ("index.jsp");<br>}<br>}</p></p><p><p>When a request is received to the controller, it forwards to the index.jsp Page.</p></p><p><p>Create a spring configuration file below the directory/web-inf/, in the format <servlet-name>-servlet.xml, for example shirdrn-servlet.xml, where SHIRDRN corresponds to the configuration in Web. XML <servlet-name>. Assemble the controller Org.shirdrn.spring.mvc.ShirdrnController implemented above in shirdrn-servlet.xml, as shown in the following configuration:</p></p><p><p><?xml version= "1.0" encoding= "UTF-8"?><br><! DOCTYPE Beans Public "-//spring//dtd bean//en" "http://www.springframework.org/dtd/spring-beans.dtd" ></p></p><p><p><beans><br><bean id= "shirdrncontroller"<br>class= "org.shirdrn.spring.mvc.ShirdrnController" abstract= "false"<br>singleton= "true" lazy-init= "default" autowire= "default"<br>dependency-check= "default" ><br></bean><br><br><bean id= "shirdrnurlmapping"<br>class= "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"<br>abstract= "false" singleton= "true" lazy-init= "default"<br>autowire= "default" dependency-check= "default" ><br><property name= "mappings" ><br><props><br><span style="color: #ff0000;"><prop key= "/shirdrn.abc" >shirdrnController</prop><br></props><br></property><br></bean><br></beans></span></p></p><p><p>above, if the request is shirdrn.abc, then call the Shirdrncontroller controller, will turn to index.jsp page, generate the View.</p></p><p><p>The configuration in Web. XML is as Follows:</p></p><p><p><?xml version= "1.0" encoding= "UTF-8"?><br><web-app version= "2.4"<br>xmlns= "http://java.sun.com/xml/ns/j2ee"<br>Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"<br>xsi:schemalocation= "HTTP://JAVA.SUN.COM/XML/NS/J2EE<br>Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "><br><br><servlet><br><servlet-name>shirdrn</servlet-name><br><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><br><load-on-startup>1</load-on-startup><br></servlet><br><br><servlet-mapping><br><servlet-name>shirdrn</servlet-name><br><url-pattern>*.abc</url-pattern><br></servlet-mapping><br><br><welcome-file-list><br><welcome-file>index.jsp</welcome-file><br></welcome-file-list></p></p><p><p></web-app></p></p><p><p>That is, when the requested URL is suffixed with. abc, the Org.springframework.web.servlet.DispatcherServlet control view forwarding is referred to by the servlet name Shirdrn.</p></p><p><p>Write a simple JSP page index.jsp, as Follows:</p></p><p><p><%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><br><%<br>String Path = Request.getcontextpath ();<br>String BasePath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";<br>%></p></p><p><p> <! DOCTYPE HTML public "-//w3c//dtd HTML 4.01 transitional//en" <br>
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