今天用springmvc搭了一个框架,将步骤以及配置记录下来。
jar包网盘地址为:http://yun.baidu.com/share/link?shareid=1109438387&uk=2836507213
项目网盘地址为: http://yun.baidu.com/share/link?shareid=3913316871&uk=2836507213
1.需要导入网盘所有的jar包
2.修改web.xml文件 (加入springmvc的配置)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?xml version= "1.0" encoding= "UTF-8" ?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee" xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version= "2.5" >
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class >
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup> 1 </load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app> |
3.加入springmvc的配置文件springmvc-servlet.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<?xml version= "1.0" encoding= "UTF-8" ?>
<beans xmlns= "http://www.springframework.org/schema/beans"
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"
xmlns:mvc= "http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http: //www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<bean class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 自动扫描Controller类 -->
<context:component-scan base- package = "com.xj.action" />
<!-- 定义视图解析器 -->
<bean id= "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "viewClass" value= "org.springframework.web.servlet.view.JstlView" />
<property name= "prefix" value= "/" />
<property name= "suffix" value= ".jsp" />
</bean>
<mvc:annotation-driven />
<mvc: default -servlet-handler/>
<mvc:resources mapping= "/asserts/**" location= "/asserts/" />
</beans> |
4.编写controller文件
1
2
3
4
5
6
7
8
9
10
11
12
|
@Controller public class HelloController {
@RequestMapping (value= "/fruitlist" )
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {
System.out.println( " hello world" );
return new ModelAndView( "/hello" );
}
} |
然后开始访问 http://localhost/TestSpringmvc/fruitlist 总是报错,原因是因为上面的controller文件引入的ModelAndView引入错误为org.springframework.web.portlet.ModelAndView;,正确应该引入 org.springframework.web.servlet.ModelAndView
到此为止,springmvc的配置就结束了。
本文转自布拉君君 51CTO博客,原文链接:http://blog.51cto.com/5148737/1614763,如需转载请自行联系原作者