spring MVC 配置rest风格URL

3 篇文章 0 订阅

最近公司要用spring MVC,所以上网学习了一下,发现很不错,这里说说怎么配置res风格的URL(如:http://localhost:8080/test/user/login)

1、首先配置web.XML

添加

<servlet>  
        <servlet-name>spring</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 这里先配置spring的请求分发器-->  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:mvc.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
</servlet>  
  
  
<servlet-mapping>  
        <servlet-name>spring</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping><!--这里要修改一下,这里本来spring一般都是“*.do”之类的xx,把这里直接修改为“/” 就行了 -->  

<!--但是这个配置后就会拦截项目里所有的文件,但是项目中的图片、样式、js等这些文件不需要进行拦截,所有还要配置静态文件-->
<servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>style/*</url-pattern>  
</servlet-mapping>
<servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>img/*</url-pattern>  
</servlet-mapping>
<servlet-mapping>  
        <servlet-name>default</servlet-name>  
        <url-pattern>js/*</url-pattern>  
</servlet-mapping>

2、配置spring.xml配置文件

<context:component-scan base-package="com.test.controller"/>  <!--配置扫描路径-->
    
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<span><span class="tag">
<</span><span class="tag-name">bean</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"</span><span class="tag">/></span><span>  
</span></span>
<span><span class="tag"><</span><span class="tag-name">bean</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"viewResolver"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"org.springframework.web.servlet.view.InternalResourceViewResolver"</span><span class="tag">></span><span>    </span></span><span>
<span class="tag">          <</span><span class="tag-name">property</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"viewClass"</span><span class="tag">></span><span>
</span></span><span><span class="tag">              <</span><span class="tag-name">value</span><span class="tag">></span><span>org.springframework.web.servlet.view.JstlView</span><span class="tag"></</span><span class="tag-name">value</span><span class="tag">></span><span>  
</span></span><span><span class="tag">         </</span><span class="tag-name">property</span><span class="tag">></span><span>  </span></span><span><span class="tag">
         <</span><span class="tag-name">property</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"prefix"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"/WEB-INF/jsp/"</span><span class="tag">/></span><span> </span></span><span><span class="tag">
         <</span><span class="tag-name">property</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"suffix"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">".jsp"</span><span class="tag">/></span><span>
</span></span><span><span class="tag"></</span><span class="tag-name">bean</span><span class="tag">></span><span> 
</span></span><span><span></span></span>

3、配置action

  1. 在类上面加上@Controller注解
  2. 在请求方法上加入@RequestMapping(value ="/user/login")
    <span><span class="annotation">@RequestMapping</span><span>(value =</span><span class="string">"/user/login"</span><span>) </span></span> 
    public ModelAndView welcome(HttpServletRequest request,HttpServletResponse response,  
                ModelMap modelMap) {  
            //这只是一个普通的转向的类  
            return new ModelAndView("welcome",modelMap);  
        }  
    

ok了,启动服务器,你就可以看到你想要的结果了!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值