为什么我的动作不能在我的javaWeb项目中工作?

时间:2017-04-04 15:25:28

标签: spring tomcat http-status-code-404

IDE版本:struts2.5.8 + spring4.3.6 + hibernate5.2.8。 我是Java Web的新工作者。 我仔细检查了我的代码后,但行动无法正常工作。 每次我在Tomcatv9.0中运行我的代码时,我的chrome web浏览器都会显示“HTTP Status 404”类型状态报告。 description:请求的资源不可用。

web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="MyStrutsApp" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  -->
    <param-value>classpath:beans.xml</param-value>
</context-param>

<filter>
    <filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

struts.xml代码:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<include file="struts-default.xml"></include>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.action.extension" value="do,action"></constant>
<constant name="struts.serve.static.browserCache" value="false">     </constant>
<constant name="struts.devMode" value="true" />    
<constant name="struts.configuration.xml.reload" value="true"></constant>    
<constant name="struts.enable.DynamicMethodInvocation" value="true" />     
<package name="registration" extends="struts-default" namespace="/" >        
    <action name="user" class="userAction" method="add">
        <result name="success">/registerSuccess.jsp</result>
        <result name="fail">/registerFail.jsp</result>
        <allowed-methods>add</allowed-methods>
    </action>        
</package>
</struts>

beens.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config />
<context:component-scan base-package="com.diyuan" />
<!-- 
    <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/spring" />
    <property name="username" value="root" />
    <property name="password" value="bjsxt" />
    </bean>
-->
<bean       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:jdbc.properties</value>
    </property>
</bean>
<bean id="dataSource" destroy-method="close"
    class="org.apache.commons.dbcp2.BasicDataSource">
    <property name="driverClassName"
        value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- 
    <property name="annotatedClasses">
        <list>
            <value>com.bjsxt.model.User</value>
            <value>com.bjsxt.model.Log</value>
        </list>
    </property>
     -->
     <property name="packagesToScan">
        <list>
            <value>com.diyuan.integration.model</value>             
        </list>
    </property>
    <property name="hibernateProperties">
        <value>
            org.hibernate.dialect.MySQL5Dialect
        </value>            
    </property>
</bean> 
<!--
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
-->
<bean id="txManager"        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>    
<bean id="userDao" class="com.diyuan.integration.dao.impl.UserDaoImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="userManager" class="com.diyuan.integration.service.impl.UserManagerImpl">
    <property name="userDao" ref="userDao"/>
</bean>
<bean id="userAction" class="com.diyuan.integration.action.UserAction" scope="prototype">
   <property name="um" ref="userManager"/>
</bean>

行动代码:

package com.diyuan.integration.action;
import javax.annotation.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import       org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import com.diyuan.integration.model.User;
import com.diyuan.integration.service.UserManager;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport { 
    private String username;
    private String password;
    private String password2;   
    private UserManager um;
    public UserManager getUm() {
    return um;
}
public void setUm(UserManager um) {
    this.um = um;
}
@Override
public String execute() throws Exception {
    User u = new User();
    u.setUsername(username);
    u.setPassword(password);        
    if(um.exists(u)) {         
        return "fail";
    }
    um.add(u);      
    return "success";
}
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 String getPassword2() {
    return password2;
}
public void setPassword2(String password2) {
    this.password2 = password2;
}   

}

1 个答案:

答案 0 :(得分:0)

今天,我再次检查我的代码,并在register.jsp页面代码中找到错误代码,如下所示:

<body>
<form method="post" action="user.Action">
    UserName:<input type="text" name="username"><br>
    Password:<input type="text" name="password"><br>
    Password2:<input type="password" name="password2"><br>
    <input type="submit" value="Login"/>
</form>    

更改大写&#34; user.Action&#34;小写&#34; user.action&#34;,然后struts动作可以工作。

<body>
<form method="post" action="user.action">
    UserName:<input type="text" name="username"><br>
    Password:<input type="text" name="password"><br>
    Password2:<input type="password" name="password2"><br>
    <input type="submit" value="Login"/>
</form>