struts2与spring都是java EE中非常流行的框架,下面就是用idea这个非常只能的java IDE工具进行spring与struts2的整合
一,新建一个Project,勾选下图红圈的两项
idea会自动下载一部分必须的jar包,并且创建好工程文件目录
二,加入缺少的jar包,idea下载了部分,但是还是缺少一些
- springframework.asm-XXXXXX.jar
- springframework.beans-XXXXXX.jar
- springframework.context-XXXXXX.jar
- springframework.expression-XXXXXX.jar
- springframework.web-XXXXXX.jar
XXXXX:spring版本号
三准备工作完了,就是写程序了
先将完整的目录结构贴出来
1.下完成web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>struts2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
2.然后是LoginAction
package per.summer.actions;
import com.opensymphony.xwork2.ActionSupport;
import per.summer.beansFactory.MyService;
/**
* Created by summer on 17-4-4.
*/
public class LoginAction extends ActionSupport{
private String username;
private String password;
//业务逻辑组件
private MyService ms;
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 MyService getMs() {
return ms;
}
public void setMs(MyService ms) {
this.ms = ms;
}
@Override
public String execute() throws Exception {
if (ms.validLogin(username, password).equals("success")) {
return SUCCESS;
} else {
return ERROR;
}
}
}
3.业务逻辑组件(MyService)与其接口
package per.summer.beansFactory;
/**
* Created by summer on 17-4-4.
*/
public interface MyService {
String validLogin(String username , String pass);
}
package per.summer.beans;
import per.summer.beansFactory.MyService;
/**
* Created by summer on 17-4-4.
*/
public class MyServiceImpl implements MyService {
public String validLogin(String username , String pass) {
if (username.equals("summer") && pass.equals("manager")) {
return "success";
} else {
return "error";
}
}
}
4.applicationContext.xml文件。该文件需要与web.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.xsd">
<bean id="myService"
class="per.summer.beans.MyServiceImpl"/>
<bean id="userClass" class="per.summer.actions.LoginAction"
scope="prototype"
p:ms-ref="myService"/>
</beans>
5.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"/>
<package name="summer" extends="struts-default">
<!--这里的userClass就不是具体的类了,他是一个spring bean在applicationContext.xml中设置的一个bean-->
<action name="login" class="userClass">
<result name="success">/WEB-INF/JSP/welcome.jsp</result>
<result name="error">/WEB-INF/JSP/error.jsp</result>
</action>
</package>
</struts>
6.index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
Created by IntelliJ IDEA.
LoginAction: summer
Date: 17-4-4
Time: 下午2:07
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<s:form action="login" method="POST">
<s:textfield name="username" label="username"/>
<s:password name="password" label="password"/>
<s:submit label="login"></s:submit>
</s:form>
</html>
welcome.jsp与error.jsp就不给出了,自己想咋写就咋写
网友评论