首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

SSH整合例子(仅用到ss处理登录demo)出现错误

2013-09-11 
SSH整合例子(仅用到ss处理登录demo)出现异常帖代码先---------------------------------public class User

SSH整合例子(仅用到ss处理登录demo)出现异常
帖代码先
---------------------------------
public class User { // 实体类

private String name;
private String password;
//省略set get方法

----------------------------------
public interface IUserService {// 接口

boolean login(User user);
boolean register(User user);
}
------------------------------------//接口实现类
public class UserService implements IUserService {
public boolean login(User user){
 
if("user".equals(user.getName())&&"user".equals(user.getPassword()))
{
return true;
}else{
return false;
}
}
public boolean register(User user){
  
if("qqq".equals(user.getName())&&"qqq".equals(user.getPassword())){
return true;
}else{
return false;}
}
}
------------------------
//action
package com.action;

import com.opensymphony.xwork2.ActionSupport;
import com.service.IUserService;
import com.vo.User;

public class UserAction extends ActionSupport {

/**
*  
*/
private static final long serialVersionUID = 1L;
private User user = new User();
private IUserService service;
 
 
public User getUser() {
return user;
}

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

public IUserService getService() {
return service;
}

public void setService(IUserService service) {
this.service = service;
}

public String login(){
System.out.println(getService());
System.out.println(user.getName());
if(service.login(user)){
return SUCCESS;
}else{
return ERROR;
}
}

 public String register(){
  System.out.println(user.getName());
if(service.register(user)){
return SUCCESS;
}else{
return ERROR;
}


}
 
}
-----------------------------------------------------下面的所有配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"  
xmlns=" http://java.sun.com/xml/ns/javaee"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:applicationContext.xml</param-value>
  </context-param>
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <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></web-app>
-------------------------
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" " http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.objectFactory" value="spring" />
<package name="default" extends="struts-default" namespace="/">



<action name="user" class="com.action.UserAction">
<result name="success">/success.jsp</result>
<result name="error">/failed.jsp</result>
</action>
</package>

</struts>  
--------------------------------------
spring 配置文件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:p=" http://www.springframework.org/schema/p"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <bean id="UserserviceImpl" class="com.service.UserService"/>
<bean id="userAction" class="com.action.UserAction" autowire="byType">
<property name="service" ref="UserserviceImpl"/>
</bean>
 
</beans>
--------------------------------jsp  


<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <base href="<%=basePath%>">
   
  <title>My JSP 'index.jsp' starting page</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>  
  This is my JSP page. <br>-------------------------------
  <br>-----------------------login form---------------------------------
  <s:form action="user!login.action" >
  input your user account:<s:textfield name="user.name"></s:textfield>
  input your user password:<s:password name="user.password"></s:password>
  <s:submit value="login in right now" />
  </s:form>  
  <br>-----------------------register form---------------------------------
  <s:form action="user!login.register" >
  input your user account:<s:textfield name="user.name"></s:textfield>
  input your user password:<s:password name="user.password"></s:password>
  <s:submit name=" " value="login in right now" />
  </s:form>
  </body>
</html>

-------------------------------------------登录异常
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
com.action.UserAction.login(UserAction.java:36)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)


sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
……………………………………………………
……………………………………………………
-------------------------------------------
就是Action中的service获取不到 怎么弄都是空的???? 表单中可以获取到输入的值

在线等! 谢谢

[解决办法]
将struts.xml中action里的class改为ApplicationContext.xml中bean 的id
<action name="user" class="userAction">
另外,这里要加上<bean id="userAction" class="com.action.UserAction" autowire="byType"scope="prototype">
[解决办法]
同意三L,ApplicationContext.xml里的bean id 应与struts.xml里action的class对应!!
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html

热点排行