使用spring的多模块Maven Web项目

时间:2013-08-19 13:36:25

标签: spring spring-mvc web rotation

SampleUI项目将有两个屏幕: 用户将输入唯一ID以登录系统。 如果在数据库中找到“用户ID”,则用户将登录。 一旦用户登录,他们将看到秒屏幕。 这个屏幕非常简单。它有四个象限,每个象限都有一个颜色。 当用户选择“旋转”时,颜色顺时针旋转。

我怀疑是旋转的关系,我怎么能在Spring做以下事情:当按下按钮旋转时,使象限旋转的颜色变大?因为我必须每次旋转时一次又一次地重新加载页面,所以我可以向用户显示旋转,我只是不知道该怎么做。这是我目前的代码:

Quadrant.java

package com.Beta.app;

public class Quadrant
{
   private String Q1Color;
   private String Q2Color;
   private String Q3Color;
   private String Q4Color;

   public Quadrant()
   {
    Q1Color = "red";
    Q2Color = "green";
Q3Color = "yellow";
Q4Color = "blue";
   }

   public void setQ1Color(String Q1Color) 
   {
      this.Q1Color = Q1Color;
   }

   public String getQ1Color()
   {
      return Q1Color;
   }

   public void setQ2Color(String Q2Color) 
   {
      this.Q2Color = Q2Color;
   }

   public String getQ2Color()
   {
      return Q2Color;
   }

   public void setQ3Color(String Q3Color) 
   {
      this.Q3Color = Q3Color;
   }

   public String getQ3Color()
   {
      return Q3Color;
   }

   public void setQ4Color(String Q4Color) 
   {
      this.Q4Color = Q4Color;
   }

   public String getQ4Color()
   {
      return Q4Color;
   }

   public void rotate()
   {
    String aux1, aux2, aux3;
    aux1 = Q2Color;
    Q2Color = Q1Color;
    aux2 = Q4Color;
    Q4Color = aux1;
    aux3 = Q3Color;
    Q3Color = aux2;
    Q1Color = aux3;
   }

}

WebAppController.java

package com.Beta.app;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;

@Controller
public class WebAppController
{
   @RequestMapping(value = "/user", method = RequestMethod.GET)
   public ModelAndView login() 
   {
      return new ModelAndView("user", "command", new User());
   }

   @RequestMapping(value = "/rotationPage", method = RequestMethod.POST)
   public String rotationPage(@ModelAttribute("SpringWeb")Quadrant quadrant, 
   ModelMap model) 
   {
  quadrant.rotate();
  quadrant.rotate();
      model.addAttribute("Q1Color", quadrant.getQ1Color());
      model.addAttribute("Q2Color", quadrant.getQ2Color());
      model.addAttribute("Q3Color", quadrant.getQ3Color());
  model.addAttribute("Q4Color", quadrant.getQ4Color());

      return "result";
   }
}

result.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Rotation Page</title>
</head>
<body style = "width:100%; height:100%; margin:0px; padding:0px;">

    <div id="center" style = "position: absolute; left: 50%; top: 40%; margin-left:-200px; margin-top:-200px">
        <center>
            <form>
                <input type="submit" value="Save" />
            </form>
        </center>
        <table>
            <tr>
                <td><div style = "width:200px; height:200px; background-color:${Q1Color}"></div></td>
                <td><div style = "width:200px; height:200px; background-color:${Q2Color}"></div></td>
            </tr>
            <tr>
                <td><div style = "width:200px; height:200px; background-color:${Q3Color}"></div></td>
                <td><div style = "width:200px; height:200px; background-color:${Q4Color}"></div></td>
            </tr>
        </table>
        <center>
            <form>
                <p></p>
                <input type="submit" value="Rotate" />
            </form>
        </center>
    </div>

</body>
</html>

user.jsp

<%@ page session="false" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Login Page</title>
</head>
<body style="width:100%; height:100%; margin:0px; padding:0px;">
    <div id="page" style="position:absolute; left:40%; top:50%; text-align:center;">
        <form:form method="POST" action="/app/rotationPage.html">
            <table>
                <tr>
                    <td><form:label path="userId">User Id</form:label></td>
                    <td><form:input path="userId" /></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="submit" value="Login"/>
                    </td>
                </tr>
            </table>  
        </form:form>
    </div>
</body>
</html>

 servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.Beta.app" />



</beans:beans>

----------------------------------------------------------------------------------
    web.xml

        <?xml version="1.0" encoding="UTF-8"?>
        <web-app version="2.5" 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_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

0 个答案:

没有答案