在整合SSH时,spring和hibernate都测试过了,就是在整合struts时出问题了,我使用struts2-convention-plugin想实现0配置,头一回儿使用没用好,结果不知道是哪里问题,一直报:
[org.apache.struts2.dispatcher.Dispatcher]Could not find action or result
/sshe/userAction!addUser.action
There is no Action mapped for namespace [/] and action name [userAction] associated with context path [/sshe]. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:37)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:554)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
UserAction.java:
package com.sshe.ation;
import java.util.Date;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import com.sshe.entity.Tuser;
import com.sshe.service.UserServiceI;
@ParentPackage("sshe")
@Namespace("/")
@Action(value="userAction")
public class UserAction {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(UserAction.class);
private UserServiceI userService;
public UserServiceI getUserService() {
return userService;
}
@Autowired//自动注入
public void setUserService(UserServiceI userService) {
this.userService = userService;
}
public void addUser(){
logger.info("进入addUser没?");
Tuser tuser = new Tuser();
tuser.setId(UUID.randomUUID().toString());
tuser.setName("zhu");
tuser.setPwd("222222");
tuser.setCreateTime(new Date());
// tuser.setModifyTime(modifyTime);
userService.save(tuser);
}
}
spring.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:config.properties" />
<!-- 自动扫描dao和service包(自动注入) -->
<context:component-scan base-package="com.sshe.dao,com.sshe.service" />
</beans>
struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 是否启用开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- 让struts2支持动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="sshe" extends="struts-default">
<!--<action name="addUser" class="com.sshe.ation.UserAction" method="addUser"></action>-->
</package>
</struts>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SysFrame</display-name>
<!-- strtus2过滤器 -->
<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>
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>Login.html</welcome-file>
</welcome-file-list>
</web-app>
然后在浏览器中访问:http://localhost:8080/sshe/userAction!addUser.action就报错。
后来在struts.xml中写了:
<action name="addUser" class="com.sshe.ation.UserAction" method="addUser"></action>
并且没在UserAction使用
@ParentPackage("sshe")
@Namespace("/")
@Action(value="userAction")
public class UserAction {
访问:http://localhost:8080/sshe/addUser.action就能成功插入数据~~
求解啊,谢谢~!
------解决思路----------------------
配置@Action(value="userAction",以下url能否访问(注释掉struts.xml中1action name="addUser"这一行)?
http://localhost:8080/userAction!addUser.action
------解决思路----------------------
<constant name="struts.convention.action.packages" value="com.sshe.action"/>用你自己的包路劲