struts、sping、ibatis整合记录

新建一个web project,感觉ibatis版本变化导致出现的问题很多

1,先搞struts2

1)引入下列jar包

Commons-logging-1.0.4.jar
Freemarker-2.3.13.jar
Ognl-2.6.11.jar
Struts2-core-2.1.6.jar
Xwork-2.1.2.jar
commons-fileupload-1.2.1.jar

2)修改WEB-INF下的web.xml文件

 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
</filter>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

3)首先新建一个登陆页面login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
    <title>登录</title> 
    <meta http-equiv="pragma" content="no-cache"> 
    <meta http-equiv="cache-control" content="no-cache"> 
    <meta http-equiv="expires" content="0">    
</head> 
<body> 
   <s:form name="form1" action="login" > 
       <s:textfield name="username" label="username" ></s:textfield> 
       <s:password name="password" label="password" ></s:password> 
       <s:submit label="submit"></s:submit> 
   </s:form> 
   <s:actionerror/> 
</body> 
</html> 


4)在根目录下添加struts.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>
     <package name="struts2" extends="struts-default"> 
        <action name="login" class="action.LoginAction"> 
            <result name="success" type="redirect">index.jsp</result> 
            <result name="input">login.jsp</result> 
            <result name="error">login.jsp</result> 
        </action> 
    </package> 
     
</struts>


5)建一个action

package action; 
import com.opensymphony.xwork2.ActionSupport; 
public class LoginAction extends ActionSupport { 
 public String username; 
 public String password; 
 public String execute(){ 
     if(!username.equals("admin")){ 
         super.addFieldError("username", "用户名错误!"); 
         return ERROR; 
        } 
        if(!password.equals("001")){ 
            super.addFieldError("password", "密码错误!"); 
            return ERROR; 
        } 
        return SUCCESS; 
    } 
    public void validate(){ 
        if(username==null||username.length()==0){ 
            super.addActionError("用户名不能为空"); 
        } 
        if(password==null||password.length()==0){ 
            super.addActionError("密码不能为空"); 
        } 
    } 
} 

6)发布toncat

打开http://127.0.0.1:8080/ibatis12/login.jsp

可以看到登录页面 了,用户、密码是admin、001

struts完成了。

2、spring

添加spring.jar包

1)修改t添加web.xml文件

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:/applicationContext*.xml</param-value>
	</context-param>


2)新建applicationContext.xml

这个位置有点蛋疼,我是加在src根目录下面的

<?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: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-2.5.xsd 
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
    
	
	<bean id="TUser" class="spring.TUser">
		<property name="username" value="小张"></property>
		<property name="allname" value="张三"></property>
		<property name="address" value="青岛市"></property>
	</bean>

</beans> 


3)添加一个测试文件SpringTest.java

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
	public static void main( String[] args ) { 
	    //加载spring配置文件,初始化IoC容器 
	    ApplicationContext ac = new ClassPathXmlApplicationContext("/applicationContext.xml"); 
	       //从容器 接管Bean 
	       TUser user = (TUser) ac.getBean("TUser"); 
	       //输出欢迎信息 
	       System.out.println( "Hello:" + user.getUsername() + ";u is in " + user.getAddress() + " ; and u is " + user.getAllname() ); 
	   } 
}

4)需要一个TUser.java

package test;

public class TUser {
	private String username; 
	private String allname; 
	private String address; 

	public String getUsername() { 
	    return this.username; 
	   } 
	   public void setUsername(String username) { 
	       this.username = username; 
	   } 
	   public String getAllname() { 
	       return this.allname; 
	   } 
	   public void setAllname(String allname) { 
	       this.allname = allname; 
	   } 
	   public String getAddress() { 
	       return this.address; 
	   } 
	   public void setAddress(String address) { 
	       this.address = address; 
	   } 

}

5)运行SpringTest文件
结果:Hello:小张;u is in 青岛市 ; and u is 张三

spring好了,

3整合struts 和spring

Struts2.1.6的lib目录中找到struts2-spring-plugin-2.1.6.jar,引入到工程中(这个暂时没做)

1)添加web.xml文件
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

2)添加applicationContext*.xml
<bean id="loginAction" class="test.LoginAction" scope="prototype">
</bean>

修改struts.xml
<action name="login" class="test.LoginAction">
改为
<action name="login" class="loginAction

启动tomcat

打开http://127.0.0.1:8080/ibatis12/login.jsp

好了,s2s整合好了,下面添加ibatis

4,做一个简单的人事管理

在更目录下,建一个map和一个domain文件包

domain下面建PO类 TDept.java 和TEmployee.java

报错

java.lang.NoSuchMethodError: com.opensymphony.xwork2.util.ValueStack.findValue

jar包冲突,搜索有两个jar包含ValueStack这个类
xwork-2.1.1.jar
xwork-coue-2.1.6.jar
把一个去掉就好了



java.lang.NoSuchMethodError: com.opensymphony.xwork2.util.ValueStack.findValue

你可能感兴趣的:(struts)