SpringMVC学习笔记二第一个小的程序

首先:我们要用springmvc来写一个helloworld的例子:

  首先我们需要导入所需要的架包:

/demo1/WebRoot/WEB-INF/lib/commons-logging-1.1.1.jar
/demo1/WebRoot/WEB-INF/lib/jstl-1.2.jar
/demo1/WebRoot/WEB-INF/lib/spring-aop-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-asm-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-batch-infrastructure-2.2.2.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-beans-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-context-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-context-support-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-core-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-expression-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-jdbc-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-retry-1.0.2.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-test-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-tx-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-web-3.1.4.RELEASE.jar
/demo1/WebRoot/WEB-INF/lib/spring-webmvc-3.1.4.RELEASE.jar

第二步:我们要在src这个文件夹下写一个配置的文件,这个文件的名字叫做:springmvc.xml.

代码如下:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">

<!-- 处理器映射器 根据url匹配bean的name 处理器映射器实现了HandlerMapping接口 -->
<bean
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

<!-- 简单url映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello1.action">hello_controller</prop>
<prop key="/hello2.action">hello_controller</prop>
<prop key="/hello3.action">hello_controller3</prop>
</props>
</property>
</bean>

<!-- 处理器适配器 实现了HandlerAdapter接口 action按照适配器要求开发 ,规则是实现Controller接口 -->
<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

<!-- 配置HttpRequestHandlerAdapter处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />

<!-- 视图解析器 解析jsp视图,默认使用jstl,要求classpath下有jstl的jar包 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 视图的前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 视图的后缀 -->
<property name="suffix" value=".jsp" />

</bean>

<!-- 配置action -->
<bean id="hello_controller" name="/helloworld.action" class="cn.com.springmvc.Hello" />

<!-- 配置action -->
<bean id = "hello_controller3" class="cn.com.springmvc.Hello1"/>

<!-- 学生信息修改 -->
<bean name="/editstudent.action" class="cn.com.springmvc.student.action.EditStudent"/>
<bean name="/editstudentsubmit.action" class="cn.com.springmvc.student.action.EditStudentSubmit"/>

</beans>

第三步:在web.xml这个文件中配置springmvc所需要的东西:

<?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/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springmvc1215_1</display-name>

<!-- 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>

<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation指定 springmvc的全局配置文件 如果 contextConfigLocation不指定,默认找配置文件名称:servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

第四步:就是写我们所需要的Java代码:

public class Hello implements Controller {

@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {

ModelAndView modelAndView = new ModelAndView();

//向页面显示一行提示信息
//下边的方法就相当于request.setAttribute(arg0, arg1)
modelAndView.addObject("message", "helloworld!!!!!!");

//指定jsp页面地址

//modelAndView.setViewName("/WEB-INF/jsp/hello.jsp");
//指定逻辑视图名,真正的视图地址:前缀+逻辑视图名+后缀
modelAndView.setViewName("hello");

return modelAndView;
}

}

第五步:就是写我们的界面这个就自己写吧。

上一篇:Arduino uno 引脚说明


下一篇:SpringMVC学习笔记(二)