springmvc之helloworlddemo小记:
首先在web.xml配置springmvc的前端控制器
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
之后配置springmvc的配置文件,文件名需于web.xml中的配置名相同,如我上方未配置则我的文件名为springmvc-servlet.xml,与上方的servlet-name相同
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--自动扫描的包路径-->
<context:component-scan base-package="com.dadao.controller"></context:component-scan>
<!--静态文件配置-->
<mvc:resources mapping="/html/**" location="/html/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/image/**" location="/image/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/fonts/**" location="/fonts/" />
<!--启用注解配置-->
<mvc:annotation-driven />
<!--配置视图过滤-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value></value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
首先,springmvc需要加spring-web以及spring-webmvc两个jar包,启动项目。
报java.lang.ClassNotFoundException: org.springframework.context.ApplicationContextAware
加入spring-context jar包
报java.lang.ClassNotFoundException: org.springframework.beans.factory.Aware
加入spring-beans jar包
报java.lang.ClassNotFoundException: org.springframework.core.env.EnvironmentCapable
加入spring-core jar包
报java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/WebDemo]]
加入commons-logging jar包
报org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/springmvc-servlet.xml]; nested exception is java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
加入spring-aop jar包
报java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext
加入spring-expression jar包
项目运行成功
添加javawenjian
@Controller
public class HelloController {
@RequestMapping(value="/hello")
public String hello(Model model){
model.addAttribute("msg","你好,spingmvc");
return "index";
}
}
浏览器输入地址/hello
出现你好,springmvc
至此,ok