spring mvc环境搭建demo

在网上找了好多关于spring mvc的环境搭建的文章,对于一个小小菜鸟而言很多问题还是解决不来的,所以自己写了第一篇博客,送给在搭建环境上遇到问题的小小菜鸟,希望在你们的学习之路上为你们带来方便。 首先要配置的就是web.xml文件,首先我配置的编码过滤器,这个是解决乱码的问题,大家可以把这段代码记下来,在以后的编程中可以直接引用到。这里不做过多的介绍。第二点,配置的servlet,这里我将名字设置为spring,因为在配置对应的xml文件时,必须命名为spring-servlet.xml于此对应。也就是说我的servlet名字为a,那么xml文件就是a-servlet.xml。第三点配置servlet-mapping,这里名字要与之前配置的servlet名字相对应,“/”代表对说有的请求进行拦截处理。第四点配置spring bean文件夹,找到对应的路径即可,这个我配置的xml文件名为applicationContext.xml稍后将贴出相关的代码。


<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" 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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- 配置编码过滤器 --><filter><filter-name>Set Character Encoding</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>utf8</param-value></init-param></filter><filter-mapping><filter-name>Set Character Encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- Spring MVC配置 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param><!-- <load-on-startup>1</load-on-startup> --> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping><!-- Spring配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/applicationContext.xml</param-value> </context-param></web-app> 

下面将一个spring-servlet.xml文件,prefix代表了搜寻的范围,suffix代表了搜寻的文件类型,package代表controller所在的文件夹,这里我写了多个文件夹的方式,供大家参考,当然可以写一个文件夹。

<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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"value="org.springframework.web.servlet.view.JstlView"/>
<propertyname="prefix"value="/WEB-INF/jsp/"/>
<property name="suffix"value=".jsp"/>
</bean>

<!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射--> <mvc:annotation-driven /> <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean --><context:component-scan base-package="controller,modal,service" /></beans>

之前提到了applicationContext.xml,下面是它的配置文件写法,大家可以直接贴过去,但是他所在路径要在web.xml中做好配置。 

<pre name="code" class="html"><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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 我们可以在其中添加我们所需要配置的bean,也可以添加相应的数据库连接和事务处理等等,方便后续拓展 -></beans>
 

从而spring mvc的配置就此结束了,但是要实现一个小的demo具体实现还是没有的,下面的代码将对demo进行讲解。说到mvc首先从.jsp文件开始这是head的内容:

<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";System.out.println(basePath);%>


这是body内容:

<formaction="<%=basePath%>test/login2"method="post"> 姓名:<inputtype="text"name="username"/></br> 密码:<inputtype="password"name="password"/></br><inputtype="submit"value="提交"/><inputtype="reset"value="重置"/></form>

跳转到对应的controller中:

 @RequestMapping("/test/login2") // 请求url地址映射,类似Struts的action-mapping public String testLogin2(String username, String password) { loginService ls = new loginService(); System.out.println(username+"----"+password); ls.insert(username,password); return "loginSuccess"; } 调用loginService 

类中的方法,这个类的代码实现如下: 

public class loginService { BaseDao db = new BaseDao(); public void insert(String username,String password) { db.insert(username,password); }}BaseDao 的代码实现如下: public void insert(String username,String password) { count = update("INSERT INTO PUBLIC.LYVEE(USERNAME, PASSWORD)VALUES('"+username+"','"+password+"')");if(count>0) System.out.println("插入数据成功"); }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值