Struts2+Spring

一、工程结构如下图:

Struts2+Spring_第1张图片

二、配置文件

(1)、所在位置: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>
 <constant name="struts.objectFactory" value="spring" />
 <include file="/com/wpq/test/config/struts_login.xml"/>
</struts>

(2)、所在位置:com.wpq.test.config,文件名称:struts_login.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="StrutsSpringDemo" extends="json-default">
    <action name="loginAction" class="loginAction">
      <result name="success" type="json"></result>
    </action>
  </package>
</struts>

(3)所在位置:src,文件名称: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"
    xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
   
  <import resource="classpath:/com/wpq/test/config/applicationContext_login.xml"/>
</beans>
(4)所在位置:com.wpq.test.config,文件名称:applicationContext_login.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"
    xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
   
  <bean id="loginService" class="com.wpq.test.service.impl.LoginServiceImpl"/>
 
  <bean id="loginAction" class="com.wpq.test.action.LoginAction" scope="prototype">
    <property name="loginService">
      <ref local="loginService"/>
    </property>
  </bean>
</beans>

三、源文件

(1)、所在位置:com.wpq.test.entity,文件名称:User.java

package com.wpq.test.entity;

import java.io.Serializable;

public class User implements Serializable {

 private static final long serialVersionUID = 1L;

 private Long id;
 
 private String name;
 
 private String password;

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 @Override
 public String toString() {
  return "User [id=" + id + ", name=" + name + ", password=" + password
    + "]";
 }
}

(2)、所在位置:com.wpq.test.action,文件名称:LoginAction.java

package com.wpq.test.action;

import com.opensymphony.xwork2.ActionSupport;
import com.wpq.service.ILoginService;
import com.wpq.test.entity.User;

public class LoginAction extends ActionSupport {

 private static final long serialVersionUID = 1L;

 private User user;

 private ILoginService loginService;
 
 public User getUser() {
  return user;
 }

 public void setUser(User user) {
  this.user = user;
 }
 
 public ILoginService getLoginService() {
  return loginService;
 }

 public void setLoginService(ILoginService loginService) {
  this.loginService = loginService;
 }

 @Override
 public String execute() throws Exception {
  
  System.out.println("*********已经进入LoginAction************");
  
  loginService.login(user);
  
  user.setId(100L);
  user.setName("wangpinqun");
  user.setPassword("CH123");
  
  System.out.println(user);
  
  return "success";
 }
}

(3)、所在位置:com.wpq.service,文件名称:ILoginService.java

package com.wpq.service;

import com.wpq.test.entity.User;

public interface ILoginService {
 
 public boolean login(User user) throws Exception;

}

(4)、所在位置:com.wpq.test.service.impl,文件名称:LoginServiceImpl.java

package com.wpq.test.service.impl;

import com.wpq.service.ILoginService;
import com.wpq.test.entity.User;

public class LoginServiceImpl implements ILoginService {

 @Override
 public boolean login(User user) throws Exception
 {

  boolean reuslt = false;
  
  System.out.println("***********已经进入LoginServiceImpl*************");
  
  System.out.println(user);
  
  try
  {
   if("wpq".equals(user.getName())&& "123".equals(user.getPassword())) reuslt = true;
   
  }catch (Exception e) {
   e.printStackTrace();
  }
  
  System.out.println("++++++++++++++++++++++++");
  
  return reuslt;
 }
}

四、js文件

(1)、jquery.js、login.js

(2)、login.js

function checkLogin()
{
 $.ajax({
  type : "GET",
  url : $("#basePath").val() + "/loginAction",
  data : {'user.name' : 'wpq','user.password':'123'},
  dataType:"json",
  success : rollback
 });
}

function rollback(jsonData)
{
 var user = jsonData.user;
 
 alert('id='+user.id+' ,name='+user.name+' ,password='+user.password);
}

 

你可能感兴趣的:(Struts2+Spring)