您的位置:首页 > 编程语言 > Java开发

(不推荐使用)springMVC基本配置+controller层实现Controller接口

2017-04-12 17:26 549 查看
基本配置:

web.xml

<!-- 配置springMVC -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- contextConfigLocation 是固定的 -->
<param-name>contextConfigLocation</param-name>
<!-- 配置读取路径-->
<param-value>classpath*:config/spring-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>


spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
<!--配置url映射的类  -->
<bean name="/test/hello" class="com.tgb.web.controller.HelloWorld" />
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>


controller

//实现Controller接口
public class HelloWorld implements Controller {
//重写handleRequest方法,spring-servlet.xml配置的映射默认会调用该重写的方法
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse arg1) throws Exception {

Map<String,Object> map = new HashMap<String,Object>();
map.put("name", "丽华");
map.put("password", "123");

return new ModelAndView("/welcome","map",map);

}

}


jsp

<div>
<c:forEach items="${map }" var="m">
${m.key } ---- ${m.value }
<br>
</c:forEach>
</div>


结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐