整理下刚入职遇到的问题

刚刚入职,主管分配了个任务。很简单的增删改查,不过前端要我自学一个Angular,任务时间:两周
学了一周多的Angular之后,页面也基本画好了。想着后台SSM一搭,逻辑一写,就基本OK。
结果SSM框架就搭了两天。根本不记得什么时候配置什么。
下面把配置文件存起来,方便以后使用

  1. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>daily</display-name>
  <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>
    <!-- 上下文监听器,监听全局spring配置 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext*.xml</param-value>
  </context-param>
  <!-- 配置过滤器 将POST请求转换为PUT和DELETE请求 -->
  <filter>
  	<filter-name>HiddenHttpMethodFilter</filter-name>
  	<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>HiddenHttpMethodFilter</filter-name>
  	<url-pattern>/*</url-pattern>
<!--   	 <servlet-name>springmvc</servlet-name> -->
  </filter-mapping>

 <!-- 跨域请求 -->  
     <filter>  
       <filter-name>SimpleCORSFilter</filter-name>  
       <filter-class>com.softeem.ssm.filter.SimpleCORSFilter</filter-class>  
       <init-param>  
           <param-name>IsCross</param-name>  
           <param-value>true</param-value>  
       </init-param>  
   </filter>  
   <filter-mapping>  
       <filter-name>SimpleCORSFilter</filter-name>  
       <url-pattern>/*</url-pattern>  
   </filter-mapping>
  <!--配置springMVC的核心前端控制器 -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc-servlet.xml</param-value>
  	</init-param>	
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<!-- "/"拦截所有除jsp结尾的请求  "/*"表示拦截所有请求(包括jsp后缀) -->
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>
  1. springmvc
<?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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

   <!-- 扫描指定的包,并识别受管组件 -->
   <context:component-scan base-package="com.softeem.ssm"/>
   
   <!-- 驱动mvc的相关注解 -->
   <mvc:annotation-driven/>
   
   <!-- 默认放行所有的静态资源:html,css,js,图片,音频,视频,字体文件等静态资源 -->
   <mvc:default-servlet-handler/>
   
   <!-- 配置视图解析器 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   	<property name="prefix" value="/"/>
   	<property name="suffix" value=".jsp"/>
   </bean>
            <!-- 接口跨域配置 -->  
      <mvc:cors>  
          <mvc:mapping path="/**"  
                       allowed-origins="*"  
                       allowed-methods="POST, GET, OPTIONS, DELETE, PUT"  
                       allowed-headers="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"  
                       allow-credentials="true" />  
      </mvc:cors>
</beans>

3.applicationContent

<?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">

	<!-- 读取属性配置 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>
	
	<!-- 配置数据源 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="url" value="${url}"></property>
		<property name="username" value="${username}"></property>
		<property name="password" value="${password}"></property>
		<property name="initialSize" value="${initialSize}"></property>
		<property name="maxActive" value="${maxActive}"></property>
		<property name="minIdle" value="${minIdle}"></property>
		<property name="maxWait" value="${maxWait}"></property>
	</bean>
	
	<!-- 配置SqlSessionFactory > SqlSession > getMapper -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="typeAliasesPackage" value="com.softeem.ssm.dto"></property>
		<property name="mapperLocations" value="classpath:com/softeem/ssm/dao/*.xml"></property>
	</bean>
	
	<!-- 配置Mapper扫描器 扫描Mapper文件以及对应接口产生实现类的bean -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 指定Mapper类所在的包 -->
		<property name="basePackage" value="com.softeem.ssm.dao"></property>
		<!-- 指定SqlSessionFactory对应的bean名称 -->
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
	
	<!-- 用于处理事务的切面bean 由spring提供的事务管理器 -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 对指定包进行受管组件扫描 -->
	<context:component-scan base-package="com.softeem.ssm"/>
	
	<!-- 配置aspectJ 自动产生代理 -->
	<aop:aspectj-autoproxy proxy-target-class="true"/>
	
	<!-- 启用基于注解的声明式事务配置 -->
	<tx:annotation-driven transaction-manager="txManager"/>
	
</beans>
  1. log4j
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# logging configuration...
log4j.logger.com.softeem.ssm=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
  1. jdbc
url=jdbc:mysql://127.0.0.1:3306/firsttest
username=root
password=123456
initialSize=5
maxActive=20
minIdle=2
maxWait=60000
  1. 跨域请求的filter类,注意导包
package com.softeem.ssm.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SimpleCORSFilter implements Filter{
	private boolean isCross = false;

    @Override
    public void destroy() {
        isCross = false;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        if (isCross) {
            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;
            System.out.println("À¹½ØÇëÇó: " + httpServletRequest.getServletPath());
            httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
            httpServletResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE");
            httpServletResponse.setHeader("Access-Control-Max-Age", "0");
            httpServletResponse.setHeader("Access-Control-Allow-Headers",
                    "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token");
            httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");
            httpServletResponse.setHeader("XDomainRequestAllowed", "1");
        }
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String isCrossStr = filterConfig.getInitParameter("IsCross");
        isCross = isCrossStr.equals("true") ? true : false;
        System.out.println(isCrossStr);
    }
}

就这样,后台框架搭建好了之后,开始写逻辑。遇到了第一个问题
主管要求restful风格编写逻辑,查了一下才了解
GET对应sql中的select
POST->insert
PUT-update
DELETE->delete

例如:

@Resource(name="userService")
UserService userservice;

@ResponseBody
@RequestMapping(value="/login", method=RequestMethod.GET)
public Map<String,Object> login(User user){
	return userservice.login(user);
}
@ResponseBody
@RequestMapping(value="/regist",method=RequestMethod.POST)
public Map<String,Object> regist(@RequestBody User user){
	return userservice.regist(user);
}

如果前端传的是json类型数据,那么post、put请求需要加上@RequestBody。get和delete则不需要加@RequestBody。

前端方面,post,put提交数据时用data而get,delete提交方式用params

在后台的请求过滤器实现类中添加put/delete请求

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值