struts2整合Spring2.5.6

一、struts2.0的包可以去http://struts.apache.org/downloads.html下载,将struts2.0 中的包(commons-logging-1.0.4.jar,freemarker-2.3.10.jar,ognl- 2.6.11.jar,struts2-core-2.1.0.jar,xwork-2.1.0.jar)和spring2.5.6中的包 (spring.jar)以及struts2-spring-plugin-2.0.14.jar包(用于struts跟spring整合)放在lib目录下

二、修改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>

三、在web.xml中配置一个监听器

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

四、在src目录下新建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" >

  </package>

</struts>

五、创建LoginAction类、LoginService接口、LoginServiceImpl类、User类。

public class LoginAction extends ActionSupport implements ModelDriven<User>  {

/**
  *
  */
private static final long serialVersionUID = 1L;

private LoginService<User> loginService;

private User user;

public String execute() throws Exception {
 
  if(loginService.checkUser(user))
   return SUCCESS;
  else
   return INPUT;
}

public LoginService<User> getLoginService() {
  return loginService;
}

public void setLoginService(LoginService<User> loginService) {
  this.loginService = loginService;
}

public User getUser() {
  return user;
}

public void setUser(User user) {
  this.user = user;
}

public User getModel() {
 
  return user;
}


}



public interface LoginService<T> {

public boolean checkUser(User user);

public boolean checkUser(String name,String password);

}



public class User implements Serializable {

/**
  *
  */
private static final long serialVersionUID = 1L;

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 class LoginServiceImpl<T> implements LoginService<T> {

public boolean checkUser(User user) {
 
  if(user.getUsername().trim().equals("tang")&&user.getPassword().trim().equals("1234"))
   return true;
  else
   return false;

}

public boolean checkUser(String name, String password) {
 
  if(name.equals("tang")&&password.equals("1234"))
   return true;
  else
   return false;
}

}



六、在WEB-INF目录下创建applicationContext.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:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">



<bean id="loginService" class="cn.com.yj.struts2.service.impl.LoginServiceImpl" ></bean>

<bean id="user" class="cn.com.yj.struts2.pojo.User" ></bean>

<bean id="loginAction" class="cn.com.yj.struts2.action.LoginAction" scope="prototype" >
<property name="loginService" ref="loginService" ></property>
<property name="user" ref="user" ></property>
</bean>

</beans>

七、创建login.jsp,success.jsp如下

<body>
    <s:form action="LoginAction" method="post" >
     <s:textfield  name="username" label="用户名:" ></s:textfield>
     <s:password  name="password" label="密码:" ></s:password>
     <s:submit label="登录" ></s:submit>
    </s:form>
  </body>



<body>
    用户名:${requestScope.user.username }<br>
    密码:${requestScope.user.password }
  </body>



八、配置struts.xml文件,内容如下:

<struts>
<package name="struts2" extends="struts-default" >
    <action name="LoginAction" class="loginAction" >
   <result name="success" >success.jsp</result>
   <result name="input" >login.jsp</result>
  </action>
</package>
</struts>



九、将其发布到tomcat,起动tomcat服务器,打开浏览器输入http://localhost:8080,输入用户和密码进行测试。


你可能感兴趣的:(spring,tomcat,jsp,xml,struts)