The use of the handlermapping of Spring Web MVC is-------simpleurlhandlermapping (there are three kinds of matching methods)

Source: Internet
Author: User
Tags xmlns

Use background:
First step >>>
Create a Controller:loginconstroller under the package Com.spring.web.controller
public class Logincontroller extends abstractcontroller{

Public Modelandview handlerequestinternal (HttpServletRequest request,
HttpServletResponse response) throws exception{

String username=request.getparameter ("UserName");
String pwd=request.getparameter ("pwd");

String msg= "";
Modelandview mav=new Modelandview ("Loginresult"); Note To create a loginresult.jsp page under/web-inf/jsp
if (!username.equals ("Liuxi")) {
Msg= "username does not exist!";
}else if (!pwd.equals ("8888")) {
Msg= "The password is incorrect!";

}else{
msg= "Congratulations on your login success!";
}


Mav.addobject ("msg", MSG);
return MAV;
}

}

Step Two >>>>
Configure Web.xml:
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.5"
Xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">


<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

</servlet>

<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Step three >>>>
Configure Sample-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-2.0.xsd ">

<bean id= "Logincontroller" class= "Com.spring.web.controller.LoginController"/>


<bean id= "handlermapping" class= "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
<!--configuration Method A
<property name= "UrlMap" >
<map>
<entry key= "/user/login.do" value-ref= "Logincontroller"/>
</map>
</property>
-->
<!--configuration Method two
<property name= "Mappings" >
<bean class= "Org.springframework.beans.factory.config.PropertiesFactoryBean" >
<property name= "Location" >
<value>urlMap.properties</value> <!--urlmap.properties files should be placed in the Webroot directory at this time! -->
</property>
</bean>
</property>
-->
<!--configuration Method three-->
<property name= "Mappings" >
<props>
<prop key= "/user/login.do" >loginController</prop>
</props>
</property>
</bean>

<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" ><value>/WEB-INF/jsp/</value></property>
<property name= "suffix" ><value>.jsp</value></property>
<property name= "Viewclass" >
<value>
Org.springframework.web.servlet.view.JstlView
</value>
</property>
</bean>


</beans>

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.