欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring MVC入门小例子

程序员文章站 2022-04-09 09:14:12
...
[b][color=green][size=large]入门Spring MVC也有一段时间了,今天写篇文章总结一下 。[/size][/color][/b]


[b][color=olive][size=large]web.xml里的配置如下:[/size][/color][/b]

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name></display-name>


<servlet>
<!-- 第一个首先调用的前端控制器,注意与WEB-INFO下servlet的xml相对应 -->
<servlet-name>chapter2</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>chapter2</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>forwarding</servlet-name>
<servlet-class>com.qin.sanxian.ForwardServlet</servlet-class>
</servlet>
<!-- 处理post提交的乱码解决 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

[b][color=green][size=large]chapter2-servlet.xml里面的配置如下:[/size]
[/color][/b]
<?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">


<!-- 第二步调用 HandlerMapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

<!--第三步调用 HandlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

<!-- 第六步调用 ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 第七步调用 到/web-info下面映射到hello.jsp,进而渲染页面 -->
<!-- 处理器 -->
<bean name="/hello" class="com.qin.sanxian.HelloWorldController">
<!-- 仅支持post提交提交表单的支持 <property name="supportedMethods" value="POST"></property>-->

</bean>
<bean name="/forward"
class="org.springframework.web.servlet.mvc.ServletForwardingController">
<property name="servletName" value="forwarding"></property>
</bean>


<bean name="/abstractCommand"
class="com.qin.sanxian.MyCommandController">


<!-- 也可以通过依赖注入 注入命令实现类 -->
<!-- property name="commandClass" value="cn.javass.chapter4.model.UserModel"/-->
</bean>
<!-- 表单的控制器 -->
<bean name="/simpleForm" class="com.register.ReginsterController">
<property name="formView" value="register"></property>
<property name="successView" value="redirect:/success"></property>
</bean>

<!-- 处理成功的页面 -->

<bean name="/success" class="com.qin.sanxian.SuccessController"></bean>


<bean name="/mutilRegister" class="com.mutilregister.MultiRegisterController">

<property name="pages">
<list>
<value>wizard/baseinfo</value>
<value>wizard/schoolinfo</value>
<value>wizard/workinfo</value>
</list>

</property>

</bean>
</beans>

[b][color=olive][size=large]控制器demo[/size][/color][/b]
package com.qin.sanxian;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
*
*hellowrod控制器
* **/
public class HelloWorldController implements Controller {




@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {

//1,收集参数,验证参数
//2,绑定参数到命令对象
//3,将命令对象传入业务对象进行业务处理
//4,选择下一个页面
//ModelAndView mv=new ModelAndView();
//添加模型数据,可以是任意的POJO对象
//mv.addObject("message", "Hellow Wordld!");
//设置逻辑视图名,视图解析器,会根据此名字,解析到对应的视图页面
/**
*
* 第五步返回 根据此名字
* */
//mv.setViewName("hello");
arg1.getWriter().write("abc");
return null;
}

}

[b][size=large][color=olive]项目的目录结构如下截图:[/color][/size][/b]

[img]http://dl2.iteye.com/upload/attachment/0092/7314/ecaa51ed-a23d-3792-94e3-0346e202d6d0.png[/img]