最近公司要用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
- 在类上面加上@Controller注解
- 在请求方法上加入@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了,启动服务器,你就可以看到你想要的结果了!