springmvc配置 新手入门教程

web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="2.5">
<servlet>
<!--通过此处的命名,spring会自动去WEB-INF/下寻找 此名+ -servlet.xml 文档
如此处,我用spring做为名字,则它会自动去找匹配的WEB-INF/spring-servlet.xml文档-->
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<!--配置一个请求后缀,凡是以html结尾的路径,都会被springmvc拦截-->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!--配置spring时需指定要加载的配置文件,文件内容可以为空-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:application-main.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>-->
<!-- <servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>-->
<!--首页-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


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.xsd">

<!--这是springmvc一种简单的请求方式,还有更多方式可以配置。这里的name就是用户
可以在前台访问的路径。如访问http://localhost:8080/index.html,则会自动
跳转到这个控制器里-->
<bean name="/index.html" class="cn.oyangk.web.action.IndexCtrl">
</bean>
</beans>


Java代码
package cn.itcchina.web.action;

import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2009-6-30
* Time: 23:14:09
*/
public class IndexCtrl implements Controller{
private final static Log logger = LogFactory.getLog(IndexCtrl.class);
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
logger.info("hello.jsp is run..");
return new ModelAndView("/WEB-INF/jsp/index.jsp");
}
}

非常简单的一个类,实现了Controller接口,(也可以使用其它方式,实现这个Action)。在这个类中需重写handleRequest()方法。这个方法直接返回一个页面。到此springmvc的配置完成了。非常简单吧,而且连log4j也自动配置好了!

所需要的jar包也非常少。最主要的加这个jar包:spring-webmvc-2.5.jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值