• spring整合struts2 以及 会遇到的问题


    导入jar包
    导入spring的jar包时可能会报jarweb冲突的错误,可以删除.jar以外的javaweb的jar包。
    导入struts2-blank中所有包即可
    要整合spring和struts需要另外加一个jar包
    Struts 2 Spring Plugin » 2.3.34
    注意,不是所有的struts2-spring-plugin:jar都可以,要选择合适的,不然可能会报错。
    struts.xml

    
    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.i18n.encoding" value="GBK"/>
    	<constant name="struts.devMode" value="true"/>
    	<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
    	<package name="default" namespace="/" extends="struts-default">
    		<action name="login" class="loginAction">
    			
    			<result name="error">/error.jspresult>
    			<result>/welcome.jspresult>
    		action>
    		<action name="index">
    			<result>/loginForm.jspresult>
    		action>
    	package>
    struts>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    applicationContext.xml

    
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns="http://www.springframework.org/schema/beans"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    
    	<bean id="myService" class="com.atliin.test.MyServiceImpl"/>
    	<bean id="loginAction" class="com.atliin.test.LoginAction" scope="prototype" p:ms-ref="myService"/> 
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    LoginAction

    package com.atlin.test;
    
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction extends ActionSupport
    {
    	// 下面是用于封装用户请求参数的两个成员变量
    	private String username;
    	private String password;
    	// 系统所用的业务逻辑组件
    	private MyService ms;
    	// 设值注入业务逻辑组件所必需的setter方法
    	public void setMs(MyService ms)
    	{
    		this.ms = ms;
    	}
    	// username的setter和getter方法
    	public void setUsername(String username)
    	{
    		this.username = username;
    	}
    	public String getUsername()
    	{
    		return this.username;
    	}
    	// password的setter和getter方法
    	public void setPassword(String password)
    	{
    		this.password = password;
    	}
    	public String getPassword()
    	{
    		return this.password;
    	}
     
    	// 处理用户请求的execute方法
    	public String execute() throws Exception
    	{
    		// 调用业务逻辑组件的validLogin()方法
    		// 验证用户输入的用户名和密码是否正确
    		if (ms.validLogin(getUsername(), getPassword()) > 0)
    		{
    			addActionMessage("哈哈,整合成功!");
    			return SUCCESS;
    		}
    		return ERROR;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    Myservice

    package com.atlin.test;
    
    public interface MyService {
    	int validLogin(String username , String pass);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    MyserviceImpl

    package com.atlin.test;
    
    public class MyServiceImpl implements MyService
    {
    	public int validLogin(String username , String pass)
    	{
    		// 此处只是简单示范,故直接判断用户名、密码是否符合要求
    		if ( username.equals("admin")
    			&& pass.equals("123456") )
    		{
    			return 99;
    		}
    		return -1;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    loginForm.jsp

    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <!DOCTYPE >
    <html >
    <head>
    	<title>登录页面</title>
    </head>
    <body>
    <h3>用户登录</h3>
    <form action="login">
    	<input name="username" />
    	<input name="password"/>
    	<input type="submit"/>
    </form>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    error.jsp

    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <!DOCTYPE>
    <html>
    <head>
    	<title>错误页面</title>
    </head>
    <body>
    	您不能登录!
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    welcome.jsp

    <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
    <!DOCTYPE>
    <html>
    <head>
    	<title>成功页面</title>
    </head>
    <body>
    	您已经登录!<br/>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    补充一个web.xml
    一定要注意web.xml是否正确,我在这里被坑了很多次

    
    <web-app 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_3_0.xsd" version="3.0">
    	
    	  <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        listener-class>   
        listener>
    	<context-param> 
    	<param-name>contextConfigLocationparam-name>
    	<param-value>classpath:applicationContext.xmlparam-value>
    	context-param>
    	
    	<filter>
    		<filter-name>struts2filter-name>
    		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
    	filter>
    	
    	<filter-mapping>
    		<filter-name>struts2filter-name>
    		<url-pattern>/*url-pattern>
    	filter-mapping>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    推荐一个很好的提供jar包和maven的网站
    点击进入
    总结:
    使用框架需要细心,配置环境不要怕麻烦,一步一步的跟着配置会遇到问题,但是配置好以后再次使用就很方便了,难的都是第一次。不过一般这种框架使用,错误的原因几乎都是jar包版本不合适。

  • 相关阅读:
    fyne - 谁说用Go不能开发应用界面
    从零开始的C语言学习第二十课:数据在内存中的存储
    TCP发送接口(如send(),write()等)的返回值与成功发送到接收端的数据量无直接关系
    Python部分异常日志缺失
    如何使用ps制作ico图标文件
    rocketMQ 安装与启动
    Leetcode刷题详解——子集
    京准、ntp校时服务器(GPS北斗卫星校时器)技术方案
    golang类型空结构体的通道
    maxwell源码编译安装部署
  • 原文地址:https://blog.csdn.net/m0_55995610/article/details/127791242