springmvc+hibernate的一个简单实例

刚刚进到新的公司,看到项目都是用webservice+springmvc的形式,之前一直用的是Struts的。觉得有必要看下且记录下来。因为之前有记录过注解注入方式,但这么久没有接触了,忘记了。还好自己有记录在博客里面。

环境:spring4.0.4.RELEASE,hibernate4.3.10Final,包都是在官网上面下载。

eclipse +mysql做的 springmvc+hibernate,采用的都是注解的形式。 

看下工程的目录

OK,先说下步骤。

eclipse先建立一个web工程,如果你用的是web3.0的话就要创建一下web.xml(默认是不创建的)

首先看下web.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/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>springmvc</display-name>
  
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:spring-beans.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 防止乱码 -->
  	<filter>
    <filter-name>encodingFilter</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>

  <!-- 如果不添加就会在hibernate4整合报错 -->
  <filter>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <filter-class>
      org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
    </filter-class>
    <init-param>
      <param-name>sessionFactoryBeanName</param-name>
      <param-value>sessionFactory</param-value>
    </init-param>
  </filter>
  
  <filter-mapping>
    <filter-name>OpenSessionInViewFilter</filter-name>
    <url-pattern>*.do</url-pattern>
  </filter-mapping>
  
  <!-- springmvc-dispatcherServlet-->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.do</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>

springmvc这个servlet的名字是和WEB-INF下面的配置文件有关联的,命名方式是[servletname]-servlet.xml,我现在这里的是springmvc。

在WEB-INF下面建立一个springmvc-servlet.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" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/mvc  
  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  <!-- 注解驱动 -->
  <mvc:annotation-driven />
  <!-- 扫描器 -->
  <context:component-scan base-package="com" />
  <!-- 视图解析器 -->
  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp"/>
  </bean>
</beans>

视图解析器分前缀后缀,然后和你的controller返回的字符串合成一个页面的路径。(都是可修改路径)

放在WEB-INF下面只是为了安全起见。

基本上springmvc需要配置就配好了。 

下面看下controller的代码:

/**
 * 
 * @author Daven.Tsang
 *
 */
@Controller
public class HelloController {

  @Resource(name="userService")
  private UserService userService;
  
  /**
   * 
   * @param name
   * @param model
   * @return
   */
  @RequestMapping(value = "/hello.do")
  public String sayHello(Model model) {
    String sql="from User";
    List<User> users = userService.findAll(sql);
    model.addAttribute("users",users);
    return "hello";
  }
}

userService是在业务注入过来的。我现在的代码是分三层,dao,service,controller(相当以前的action)也可以说是view。只是用控制器来控制视图。

返回值hello是刚刚上面所说,和前缀+hello+后缀一起定义一个页面的路径。

看下index.jsp和hello.jsp

index.jsp如下: 

<form action="hello.do" method="post">

name:<input type="text" name="user_name" id="user_name"/>

<br>

password:<input type="password" name="user_pwd" id="user_pwd"/>

<input type="submit" value="submit"/>

</form>

如果你的user类有这些属性的话,就直接在controller可以拿到参数。(自动封装)

hello.jsp

<c:forEach items="${users}" var="s">

${s.user_name}

</c:forEach>

具体下载项目下来看吧。

基本上这样一个项目就搭好了。

下载地址是 http://down.51cto.com/data/2075935 ,里面包含jar,直接导入eclipse就能用。要1颗下载豆噢,哈哈~!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值