首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

s2sh的框架调整

2012-10-07 
s2sh的框架整合struts2,spring和hibernate的整合开发。三者的整合主要是利用spring这个中间框架,向上整合st

s2sh的框架整合
    struts2,spring和hibernate的整合开发。三者的整合主要是利用spring这个中间框架,向上整合struts2,向下整合hibernate。具体的配置如下。
    struts-config.xml:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- 默认的视图主题 -->    <constant name="struts.ui.theme" value="simple" />    <constant name="struts.objectFactory" value="spring" />    <constant name="struts.devMode" value="false" />    <constant name="struts.convention.default.parent.package"   value="defaultpackage" />     <constant name="struts.convention.package.locators" value="action" /> <package name="test"   extends="struts-default"><action name="login" name="code"><?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 --><context-param>   <param-name>contextConfigLocation</param-name>   <param-value>classpath:applicationContext.xml</param-value></context-param><!-- 对Spring容器进行实例化 --><listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>   <filter>        <filter-name>struts2</filter-name>        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>struts2</filter-name>        <url-pattern>/*</url-pattern>   </filter-mapping> <filter>               <filter-name>OpenSessionInViewFilter</filter-name>               <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>       </filter>      <filter-mapping>               <filter-name>OpenSessionInViewFilter</filter-name>               <url-pattern>/*</url-pattern>       </filter-mapping>          <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

LoginAction.java:
package action;import java.util.List;import entity.Usertable;import service.LoginService;public class LoginAction {private LoginService loginService;public LoginService getLoginService() {return loginService;}public void setLoginService(LoginService loginService) {this.loginService = loginService;}private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}private Integer age;public String execute()throws Exception{Integer i=2;Usertable u=loginService.findByid(i);/*List s=loginService.findByall();*/return "success";}}

index.jsp:
<html>  <head>    <base href="<%=basePath%>">        <title>保存用户</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>  <body>  <form action="login.action" method="post">   <p>请输入下列信息:</p>   用户名:<input type="text" name="name">  密码:<input type="password" name="pass"><br>  你的年龄<input type="text" name="age">  <input type="submit" name="a" value="提交">  </form>  </body></html>

  基本上大致是这样,自己要添加相应的service和serviceImpl。dao层和entity的可以自己反向工程下。整合的结果首先看控制台没有报错,然后可以自己建立个数据库,填入数据测试一下。

热点排行