eclipse-整合struts和spring-maven

来源:互联网 发布:淘宝号信誉度查询 编辑:程序博客网 时间:2024/05/19 20:39

maven配置文件

<!-- servlet -->  <dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.0.1</version></dependency><!-- Spring-context --><dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-context</artifactId>        <version>4.2.2.RELEASE</version>    </dependency>    <!-- spring web -->    <dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.2.2.RELEASE</version></dependency><!-- struts --><dependency>    <groupId>org.apache.struts</groupId>    <artifactId>struts2-core</artifactId>    <version>2.3.24.1</version></dependency><!-- struts-spring-plugin --><dependency><groupId>org.apache.struts</groupId><artifactId>struts2-spring-plugin</artifactId><version>2.3.24.1</version></dependency>

spring配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="    http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"    default-autowire="byType"><bean id="mydata" class="webapp.Data"><property name="name" value="tian"></property><property name="age" value="23"></property></bean></beans>

struts配置文件

<?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE struts PUBLIC      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"      "http://struts.apache.org/dtds/struts-2.0.dtd">  <struts>       <package name="default" namespace="/" extends="struts-default">          <action name="login" class="mydata" method="execute">            <result name="success">index1.jsp</result>           </action>       </package>  </struts>


web.xml配置文件

  <!-- struts配置 -->  <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>  <!-- spring 配置文件 -->  <context-param>     <param-name>contextConfigLocation</param-name>     <param-value>WEB-INF/classes/applicationContext.xml</param-value>  </context-param>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>


Data.Class

public class Data {String name;int age;public void setName(String name){System.out.println(name);this.name=name;}public String getName(){return name;}public void setAge(int age){System.out.println(age+"------------------------------------");this.age=age;}public int  getAge(){return age;}public String execute(){ActionContext.getContext().getApplication().put("name",name);ActionContext.getContext().getApplication().put("age", age);ActionContext.getContext().getSession().put("name",name);ActionContext.getContext().getSession().put("age", age);System.out.println(name+age);return "success";}}


index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body>hello boy<form action="login.action" method="post"><input type="text" name="name" id="name"></input><input type="text" name="age" id="age"><input type="submit" name="sub" value="提交"></form><%out.println("hello JSP");String str="hello ";%><%=str %><% out.println("jdf<br>");application.setAttribute("hello", "hello");%></body></html>


index1.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>     <%@ taglib prefix ="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body>hello boy<form action="TestServlet" method="get"><input type="text" name="name" id="name"></input><input type="submit" name="sub" value="提交"></form><%out.println("hello JSP");String str="hello ";%><%=str %><% out.println("jdf<br>");%><%String string=request.getParameter("name");out.println(string);String hello=request.getParameter("age");out.println(hello);%><br><h1>ok</h1><%String name=(String)application.getAttribute("name");Integer age=(Integer)application.getAttribute("age");out.println(name);out.println(age);%><s:property value="name"/><s:property value="age"/><h1>ok</h1><%String string1=(String)session.getAttribute("name");out.println(string1);Integer hello1=(Integer)session.getAttribute("age");out.println(hello1);%></body></html>




0 0