struts2Demo

一,工程结构

struts2Demo_第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>
 <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="Struts2Demo" extends="json-default">
    <action name="loginAction" class="com.wpq.test.action.LoginAction">
      <result name="success" type="json"></result>
    </action>
  </package>
</struts>

三、源文件

(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.test.entity.User;
import com.wpq.test.service.ILoginService;
import com.wpq.test.service.impl.LoginServiceImpl;

public class LoginAction extends ActionSupport {

 private static final long serialVersionUID = 1L;

 private User user;

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

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

 @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.test.service,文件名称:ILoginService.java

package com.wpq.test.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.test.entity.User;
import com.wpq.test.service.ILoginService;

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);
}

 

你可能感兴趣的:(struts2Demo)